当运行这段代码时...
-(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];
}
}];
}
之前有 5 到 10 秒的延迟pushMainController
实际上被调用,即使帐户信息几乎立即被记录(在预授权之后)。如果我移动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
也打电话。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)