What is the best way of displaying a UIAlertView from a block?
I have the following action in my code :
- (IBAction)connectWithTwitterClicked:(id)sender {
ACAccountStore * account = [[ACAccountStore alloc]init];
ACAccountType * accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted == YES){
NSLog(@"Twitter account has been granted");
NSArray * arrayOfAccounts = [account accountsWithAccountType:accountType];
if([arrayOfAccounts count] > 0){
ACAccount * twitterAccount = [arrayOfAccounts lastObject];
NSLog(@"Found twitter Id of [%@]", twitterAccount.username);
// continue on to use the twitter Id
} else {
// no twitter accounts found, display alert to notify user
}
} else{
NSLog(@"No twitter accounts have been granted");
// no twitter accounts found, display alert to notify user
}
}];
}
I've tried these solutions so far :
- On either of the 2 commented lines, directly create and show a UIAlertView, this crashes the application, I believe this is due to the block being an asynchronous process and not having access to the UI thread to display the alert
- Created an NSMutableString outside the block, marked it with
__block
, set it on the commented lines and then displayed after. Similar problem here whereby the block is run asynchronously so at the time of displaying the alert theres no guarantee the NSMutableString has been set.
Can anyone suggest a solution? I want to be able to notify the user somehow so they can either not use twitter, or to go off and setup an account in the device settings.
Thanks