ACAccountType始终显示0个帐户

Min*_*arg 5 facebook objective-c ios

我已经使用facebook SDK与facebook进行身份验证.如果在设置中没有启用Facebook帐户,则单击按钮应用程序将进入safari进行身份验证,即

if (appDelegate.session.state != FBSessionStateCreated)
    {
        appDelegate.session = [[FBSession alloc] init];
    }

    [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                     FBSessionState status,
                                                     NSError *error) {
        // and here we make sure to update our UX according to the new session state
        [self updateView];
    }];
Run Code Online (Sandbox Code Playgroud)

否则,如果启用了Facebook帐户,那么

    NSArray *fbAccounts=nil;
    ACAccountType *accountTypeFB;
    if ((_accountStore = [[ACAccountStore alloc] init]) &&
        (accountTypeFB = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook] ) ){
        fbAccounts = [_accountStore accountsWithAccountType:accountTypeFB];
        NSLog(@" %d fbAccounts",[fbAccounts count]);
    }
    if ([fbAccounts count]!=0) {

        [FBSession openActiveSessionWithAllowLoginUI:YES];
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"email",
                                nil];

        [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
            if (error) {
                NSLog(@"Failure");
                NSLog(@"error %@",error);
            }
            else
            {
                NSLog(@" active session opened ");
                if (self.gotUserDetails)
                {
                    return;
                }
                [self fetchuserinfo];
            }
        }];
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 }

此图像显示访问用户信息的警报.它在模拟器中工作正常.但在设备中,它总是进入safari或Facebook应用程序进行身份验证,即使在设置中启用了Facebook帐户.请帮我找出这个问题.

谢谢大家.

Wai*_*ain 1

您不能只调用accountTypeWithAccountTypeIdentifier访问 FB 帐户,您需要提供属性。这需要使用requestAccessToAccountsWithType:options:completion:异步响应来完成。

将基于网络的备份身份验证转移到其他方法,然后检查是否可以按原样创建帐户存储。如果没有,请调用 Web 方法。如果可以,请尝试使用 访问 FB 帐户requestAccessToAccountsWithType:options:completion:。在完成块中,您要么被授予访问权限,要么调用 Web 方法。

这段代码应该全部在主线程上运行。异步响应可能会在不同的线程上调用,因此请切换回主线程以完成处理。


填写...零件。

- (void)webAuthorise {...}

- (void)authorise {
if(!_accountStore)
    _accountStore = [[ACAccountStore alloc] init];

ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

if (facebookTypeAccount == nil) {
    NSLog(@"Failed, No account");
    [self webAuthorise];
    return;
}

[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                       options:@{ACFacebookAppIdKey: @"...", ACFacebookPermissionsKey: @[@"email"]}
                                    completion:^(BOOL granted, NSError *error) {
                                        dispatch_async(dispatch_get_main_queue(), ^{
                                        if(granted){
                                            NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
                                            _facebookAccount = [accounts lastObject];
                                            NSLog(@"Success");

                                            [self ...];
                                        } else {
                                            NSLog(@"Failed, Error: %@", error);

                                            [self webAuthorise];
                                        }
                                        });
                                    }];
}
Run Code Online (Sandbox Code Playgroud)