所以我一直在尝试为网站创建一个应用程序,并且我已经获得了#34;登录"页面工作,除非它不能转换到下一个视图。
这是我认为导致问题的代码:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//NSLog(@"%@", str);
if ([str rangeOfString:@"The username or password you provided is invalid. Please try again."].location == NSNotFound) {
loginPageStatusLabel.text = @"Correct";
NSLog(@"Correct Login");
[self performSegueWithIdentifier:@"toHome" sender:self];
} else {
loginPageStatusLabel.text = @"Incorrect";
NSLog(@"Login Failed");
}
}];
*断言失败 - [UIKeyboardTaskQueue waitUntilAllTasksAreFinished],/ SourceCache / UIKit_Sim / UIKit-2935.137 / Keyboard / UIKeyboardTaskQueue.m:368 2014-05-11 00:06:51.426 LoginTests [3381:3e03] * 由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:' - [UIKeyboardTaskQueue waitUntilAllTasksAreFinished]&#39 ;可能只能从主线程调用。' waitUntilAllTasksAreFinished]'可能只能从主线程调用。
每当我尝试"登录"时,就会抛出错误。如果我独自运行,那么Segue就会工作,所以我假设问题是应用程序试图在它准备就绪之前进入下一个View并导致错误。
我是Obj-C的新手,所以如果我没有张贴足够的信息或者没有用正确的名字打电话,请通知我。
谢谢!
答案 0 :(得分:3)
我不知道您为queue
参数提供了什么值,但鉴于您的完成块正在执行必须在主线程上进行的UI更新,您可以使用[NSOperationQueue mainQueue]
(或手动将此代码分派到主队列)。此queue
参数指定应将完成块添加到哪个队列,并且因为您在完成块中执行与UI相关的操作,所以必须在主线程上完成此操作。
更正后,如果您仍然有断言错误,可以添加exception breakpoint,这将有助于确切地确认发生此断言错误的位置。或者查看堆栈跟踪。
除了使用[NSOperationQueue mainQueue]
之外,我还建议做一些更强大的错误处理:
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (!data) {
// for example, no internet connection or your web server is down
NSLog(@"request failed: %@", error);
return;
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
int statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
// for example, 404 would mean that your web site said it couldn't find the URL
// anything besides 200 means that there was some fundamental web server error
NSLog(@"request resulted in statusCode of %d", statusCode);
return;
}
}
// if we got here, we know the request was sent and processed by the web server, so now
// let's see if the login was successful.
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// I'm looking for "Welcome" ... I doubt that's right (but I don't have access to
// your web server, so I'm guessing). But the idea is that you have to find whatever
// appears after successful login that is not in the response if login failed
if ([responseString rangeOfString:@"Welcome"].location != NSNotFound) {
loginPageStatusLabel.text = @"Correct";
NSLog(@"Correct Login");
[self performSegueWithIdentifier:@"toHome" sender:self];
} else {
loginPageStatusLabel.text = @"Incorrect";
NSLog(@"Login Failed");
}
}];