我正在编写代码来访问用户的Twitter帐户,但在处理设备上没有帐户的情况下遇到了问题。我想要做的是显示一个警告,告诉用户为了向Twitter进行身份验证,他们首先需要在自己的设置中创建帐户。
下面是我的代码:
self.accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountTypeTwitter = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[self.accountStore requestAccessToAccountsWithType:accountTypeTwitter options:nil completion:^(BOOL granted, NSError *error) {
if(granted) {
//Doesn't matter for the present case
} else if (error){
[SVProgressHUD dismiss]; //not directly relevant to the present question, but it also doesn't work within the block, so I'm leaving it as a clue
switch ([error code]){
case (6):
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No Twitter Account Detected" message:@"Please go into your device's settings menu to add your Twitter account." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show]; //triggers an error
break;
}
}
}];我不是Xcode调试器的专家,但显然错误发生在ACAccountStoreReply线程中,在调用alert show之后大约有25次调用,在一个名为ucol_getVersion的进程中。调试器状态为EXC_BAD_ACCESS(code=2,address=0xcc)。
我在Stack Overflow和整个互联网上搜索了关于UIAlertViews不能在块中工作的解决方案,还搜索了显示警报的一般问题(我尝试了不同的委托值),还有对requestAccessToAccountsWithType的调用的一般问题。
我还试图通过阅读各种在线资源和Objective-C第四补充中的编程相关页面来温习我对块的理解。
任何帮助都是非常感谢的。
发布于 2013-02-25 05:16:40
您应该始终确保任何UI交互都是在主线程上完成的。将您的UIAlertView调用包装在dispatch_async中
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Twitter Account Detected" message:@"Please go into your device's settings menu to add your Twitter account." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
});在通话结束时,您也有两个nil。
https://stackoverflow.com/questions/15056740
复制相似问题