Objective C https 请求在恢复主线程之前不等待响应
Objective C https requests not waiting response before resuming main thread
我的 https 请求有问题(我认为是问题所在)。我的问题是我有一个登录页面询问我编写的 php Web 服务,以检查尝试登录的用户是否可以这样做。
从 Xcode 启动应用程序时,HTTPS 请求工作正常,但经过苹果验证后,我在 App Store 上发布了该应用程序,但不知何故,当我从 App Store 下载它时,当我按下启动请求的登录按钮时,应用程序冻结...
这是我的请求代码:
NSString *url = [NSString stringWithFormat:@"https://cataloguetest.tabuleo.fr/Appmobile/webServ_firstCo.php?username=%@",username];
__block BOOL endOfBlock = false;
__block NSArray *ret;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:url]];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
NSString *strData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data received: %@", strData);
NSError *jsonerror;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonerror];
NSString *validity =[jsonDict objectForKey:@"valid"];
NSLog(@"get validity ok..");
NSString *userInbase = [jsonDict objectForKey:@"usrIn"];
NSLog(@"validity: %@",validity);
NSLog(@"inBase: %@", userInbase);
ret = @[validity ,userInbase];
endOfBlock = true;
}] resume];
while (!endOfBlock) {};
感谢您的帮助。
好的,我设法找到了几乎完美的方法:
NSString *urlCheck = [NSString stringWithFormat:@"YOUR URL"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:urlCheck]];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
//do things with request's return
}] resume];
while(!postRequestReturned){
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
NSLog(@"REQUEST resumed main thread");
我的 https 请求有问题(我认为是问题所在)。我的问题是我有一个登录页面询问我编写的 php Web 服务,以检查尝试登录的用户是否可以这样做。 从 Xcode 启动应用程序时,HTTPS 请求工作正常,但经过苹果验证后,我在 App Store 上发布了该应用程序,但不知何故,当我从 App Store 下载它时,当我按下启动请求的登录按钮时,应用程序冻结...
这是我的请求代码:
NSString *url = [NSString stringWithFormat:@"https://cataloguetest.tabuleo.fr/Appmobile/webServ_firstCo.php?username=%@",username];
__block BOOL endOfBlock = false;
__block NSArray *ret;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:url]];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
NSString *strData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data received: %@", strData);
NSError *jsonerror;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonerror];
NSString *validity =[jsonDict objectForKey:@"valid"];
NSLog(@"get validity ok..");
NSString *userInbase = [jsonDict objectForKey:@"usrIn"];
NSLog(@"validity: %@",validity);
NSLog(@"inBase: %@", userInbase);
ret = @[validity ,userInbase];
endOfBlock = true;
}] resume];
while (!endOfBlock) {};
感谢您的帮助。
好的,我设法找到了几乎完美的方法:
NSString *urlCheck = [NSString stringWithFormat:@"YOUR URL"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:urlCheck]];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
//do things with request's return
}] resume];
while(!postRequestReturned){
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
NSLog(@"REQUEST resumed main thread");