运行此代码时...
-(IBAction)loginWithTwitter:(id)sender {
NSLog(@"Logging in with twitter");
ACAccountStore *accountStore = [[ACAccountStore alloc]init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (error) {
[self showError:error];
return;
}
if (granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 1) {
NSLog(@"Multiple twitter accounts");
}
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSLog(@"%@", twitterAccount);
[self pushMainController];
}
}];
}
在实际调用
pushMainController
之前会有 5-10 秒的延迟,即使帐户信息几乎立即被记录(在预授权之后)。但是,如果我将 pushMainController
调用移到块之后,它会立即发生,唯一的问题是用户此时不一定登录。我知道由于网络连接等变量,块可能需要一秒钟才能做出响应,但有人可以帮助我理解这一点吗?
主队列上未完成完成块。您需要确保您的 UI 代码在主线程上完成:
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (error) {
[self showError:error];
return;
}
if (granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 1) {
NSLog(@"Multiple twitter accounts");
}
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSLog(@"%@", twitterAccount);
dispatch_async(dispatch_get_main_queue(), ^{
[self pushMainController];
});
}
}];
您可能还需要结束
showError
通话。