0

运行此代码时...

-(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在块之后移动呼叫,它会立即发生,唯一的问题是用户当时不一定登录。我知道由于网络连接等变量,一个块可能需要一秒钟才能得到响应,但是有人可以帮助我理解这一点吗?

4

1 回答 1

0

完成块未在主队列上完成。您需要确保您的 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通话。

于 2013-03-22T05:51:18.683 回答