6

我正在编写代码以访问用户的 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 的一般问题。

我还尝试通过阅读各种在线资源以及 Programming in Objective-C, 4th add 的相关页面来加深对块的理解。

任何帮助表示赞赏。

4

1 回答 1

22

您应该始终确保在主线程上完成任何 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通话结束时您还有两个s。

于 2013-02-24T21:16:39.903 回答