简体   繁体   English

UIAlertView在完成处理程序块中不起作用

[英]UIAlertView doesn't work in completion handler block

I'm writing code to gain access to a users Twitter account, but am having trouble handling the case where no accounts are on the device. 我正在编写代码以访问用户的Twitter帐户,但是在处理设备上没有帐户的情况时遇到了问题。 What I'd like to do is display an alert telling the user that in order to authenticate with Twitter, they'll first need to create the account in their settings. 我想做的是显示警告告诉用户,为了通过Twitter进行身份验证,他们首先需要在他们的设置中创建帐户。

Here's my code: 这是我的代码:

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;

        }
    } 
}];

I'm no expert with the Xcode debugger, but apparently the error is occurring in the ACAccountStoreReply thread, about 25 calls deep after [alert show] is called, in a process called ucol_getVersion. 我不是Xcode调试器的专家,但显然错误发生在ACAccountStoreReply线程中,在调用[alert show]之后大约25次调用,在一个名为ucol_getVersion的进程中。 Debugger states EXC_BAD_ACCESS(code=2, address=0xcc). 调试器指出EXC_BAD_ACCESS(代码= 2,地址= 0xcc)。

I've searched on Stack Overflow and the internet at large for solutions regarding UIAlertViews not working in blocks, and also for general problems showing alerts (I've tried different values for the delegate), and also for general problems with calls to requestAccessToAccountsWithType. 我已经在Stack Overflow和整个互联网上搜索了UIAlertViews无法在块中工作的解决方案,以及显示警报的一般问题(我为委托尝试了不同的值),以及调用requestAccessToAccountsWithType的一般问题。

I've also attempted to brush up on my understanding of blocks by reading various online resources, and the relevant pages of Programming in Objective-C, 4th addition. 我还尝试通过阅读各种在线资源以及Objective-C编程的相关页面,第四次补充来了解我对块的理解。

Any help is appreciated. 任何帮助表示赞赏。

You should always make sure any UI interaction is done on the main thread. 您应始终确保在主线程上完成任何UI交互。 Wrap your UIAlertView call in a dispatch_async : dispatch_async包装您的UIAlertView调用:

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];
});

You also had two nil s at the end of the call. 你在通话结束时也有两个nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM