我是iOS开发的新手,我一直坚持这个bug。我正在制作一个使用网络服务的简单应用程序。现在我目前有两个视图控制器。登录视图控制器(带有NIB文件)和主视图控制器(带有NIB文件)。当我创建应用程序时,我选择了一个空的应用程序,所以我没有故事板。相反,我正在使用UINavigationController
。当我运行我的代码后,输入我的用户名和密码并在登录视图中按提交后出现以下错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
这是我提交按钮的代码:
-(IBAction)logIn:(id)sender{
UIApplication *application = [UIApplication sharedApplication];
application.networkActivityIndicatorVisible = YES;
[_loginNetworkingContorller checkCredentialsWithUsername:self.username.text withPassword:self.password.text completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(!error){
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (httpResp.statusCode == 200) {
NSLog(@"SUCESS");
NSDictionary *credentials = @{self.username.text: self.password.text};
[KeychainUserPass save:@"MY APP" data:credentials];
UIViewController *mainView = [[RDMainViewController alloc] initWithNibName:@"RDMainViewController" bundle:nil];
[self.navigationController pushViewController:mainView animated:YES];
}
else{
NSLog(@"ERROR");
}
}
else{
NSLog(@"ERROR");
}
}];
}
这是以下函数的代码
[_loginNetworkingContorller checkCredentialsWithUsername:self.username.text withPassword:self.password.text completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
-(void)checkCredentialsWithUsername:(NSString *)username withPassword:(NSString *)password completionHandler:(void (^)(NSData *data,NSURLResponse *response, NSError *error))myCompletion
{
NSString *requestString = @"SOME WEBSITE";
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSData *userPasswordData = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64EncodedCredential = [userPasswordData base64EncodedStringWithOptions:0];
NSString *authString = [NSString stringWithFormat:@"Basic %@", base64EncodedCredential];
NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.HTTPAdditionalHeaders=@{@"Authorization":authString};
self.session=[NSURLSession sessionWithConfiguration:sessionConfig];
NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
myCompletion(data, response, error);
}];
[dataTask resume];
}
我现在陷入困境,并不确定问题是什么,特别是因为我对键盘没有做任何事情。我觉得我的积木存在问题,但我不确定问题是什么。任何帮助将不胜感激。
答案 0 :(得分:2)
嘿,在我收到网络电话的回复后,我遇到了同样的问题。我能够解决它做以下事情:
//do something with response
dispatch_async(dispatch_get_main_queue()) { () -> Void in
// continue with program by calling next step on main thread
}
我认为如果你推到下一个视图控制器iOS试图在不是主线程上做,导致错误,但我不是100%是准确的