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スレッドで発生しており、ucol_getVersionと呼ばれるプロセスで[alertshow]が呼び出されてから約25回の呼び出しがあります。デバッガーはEXC_BAD_ACCESS(code = 2、address = 0xcc)を示します。

私はStackOverflowとインターネット全体で、UIAlertViewsがブロックで機能しないことに関する解決策、アラートを表示する一般的な問題(デリゲートに対してさまざまな値を試しました)、およびrequestAccessToAccountsWithTypeの呼び出しに関する一般的な問題を検索しました。

また、さまざまなオンラインリソース、および4番目に追加されたObjective-Cのプログラミングの関連ページを読んで、ブロックの理解を深めようとしました。

どんな助けでも大歓迎です。

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また、通話の最後に2つの秒がありました。

于 2013-02-24T21:16:39.903 に答える