所以我正在尝试使用ACAccountStore登录/注册用户。这是使用以模态方式呈现的视图控制器发生的。它工作得很好,但是,当我关闭视图控制器时,底层/呈现视图控制器仍然是一个黑色窗口。我认为这种情况发生了,因为我不等待完成块完成。
所以我的问题是:在调用[self dismissViewControllerAnimated:YES completion:nil];
之前,如何等待完成块完成?
-(void)loginWithTwitter{
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error)
{
if (granted) {
//do something -> call function to handle the data and dismiss the modal controller.
}
else{
//fail and put our error message.
}
}];
}
答案 0 :(得分:2)
完成块是在主要进程(在这种情况下访问帐户请求)完成后执行的事情。所以你可以把[self dismissViewControllerAnimated:YES completion:nil]
放进去。
另一件事:由于保留周期,在块中引用self
是不好的。您可以将代码修改为如下所示:
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierTwitter];
__weak UIViewController *weakSelf = self;
[account requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error) {
[weakSelf dismissViewControllerAnimated:YES completion:nil];
if (granted) {
//do something -> call function to handle the data and dismiss the modal controller.
}
else {
//fail and put our error message.
}
}];