ios - 有时登录需要很长时间

标签 ios objective-c httprequest

我是 iOS 应用程序开发的新手。我在登录页面有一个问题。 有时登录需要很长时间。我正在使用这段代码发送或接收来自 httpserver 的请求。

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonData1
                                                   options:0 // Pass 0 if you don't care about the readability of the generated string
                                                     error:&error];
if (!jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    jsonString= [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"converted json string is %@",jsonString);
}

NSData *postData = [[[NSString alloc] initWithFormat:@"method=methodName&email=%@&password=%@", user_name, pass_word] dataUsingEncoding:NSASCIIStringEncoding ];
NSString *postLength = [NSString stringWithFormat:@"%ld",[postData length]];


jsonData=[jsonString dataUsingEncoding:NSASCIIStringEncoding];
NSLog(@"the final passing json data is %@",jsonData);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http:urladdress"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"\"Accept\""];
[request setValue:@"application/json" forHTTPHeaderField:@"\"Content-Type\""];
[request setValue:postLength forHTTPHeaderField:@"\"Content-Length\""];
[request setValue:@"application/x-www-form-urlencoded;" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];

NSError *requestError = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request
                                        returningResponse:&response
                                                    error:&requestError];

//if communication was successful

if ([response statusCode] >= 200 && [response statusCode] < 300) {
    NSError *serializeError = nil;

    NSString* newStr = [NSString stringWithString :[urlData bytes]];

    NSDictionary *jsonData = [NSJSONSerialization
                              JSONObjectWithData:urlData
                              options:NSJSONReadingAllowFragments
                              error:&serializeError];
    NSLog(@"recdata %@",jsonData);
}
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (connection)
{
    NSLog(@"theConnection is succesful");
    self.receivedData = [NSMutableData data];
}
[connection start];

[self readFromDataBase];
if (dataCheck==true) {
    [self checkPassword];
}

有什么方法可以更快地登录?

最佳答案

可能由于您的服务器或您的连接质量导致连接缓慢。

你尝试过异步吗?等待响应时不会卡住您的应用

Asynchronous NSURLConnection Scheme Tutorial

对于您的程序,替换 sendSync 方法:

NSData *urlData = [NSURLConnection sendSynchronousRequest:request
                                        returningResponse:&response
                                                    error:&requestError];

通过 sendAsync 方法:

    NSOperationQueue *mainQueue = [[NSOperationQueue alloc] init];
    [mainQueue setMaxConcurrentOperationCount:5];    

    [NSURLConnection sendAsynchronousRequest:request queue:mainQueue completionHandler:^(NSURLResponse *response, NSData *urlData, NSError *requestError) {
        // doing somethings ...
        // if communication was successful ...
    }];

关于ios - 有时登录需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31958195/

相关文章:

iOS:分发版本是否应该在我的开发者设备上运行?

ios - 如何从 Iphone 中的另一个 uiview 调用协议(protocol)?

objective-c - "Unrecognized Selector"- 将 AppDelegate 的 NSManagedObjectContext 导入其他类时出错(OSX)

ios - Swift 中的 super 初始化构造函数

javascript - Angular http 请求缺少 post 值

java - 如何通过 HTTP get 请求发送授权 key ?

grails - 在 Grails 中,如何获取包含请求参数的请求 URI?

ios - 问题理解 CALayer 几何。需要帮助

ios - 将内存中的 UIimage 上传到 iOS 中的 Dropbox

iphone - 我们可以使用 gamecenter 在 iPhone 和 iPad 之间在线玩游戏吗?