アプリなどを開発するブログ

React Native / Swift / Ruby on Railsなどの学習メモ。


Blocks内でUIActionSheetやUIAlertVIewを表示したいとき

何か処理をした後にblocksのcompletion blocksハンドラ内で
UIAlertViewやUIActionSheetを出したい場合、普通に実装しても出て来ません。

解決策として、メインスレッドで実行するようにすれば、
通常通り出ます。

ACAccountStore* accountStore = [[ACAccountStore alloc] init];
ACAccountType* type = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

[accountStore requestAccessToAccountsWithType:type options:nil completion:^(BOOL granted, NSError *error) {
    if (!granted) {
         // メインスレッドで実行
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"アラート" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        });
    }
}];

これでOKです。