ACAccountType始终显示0个帐户

时间:2013-06-18 07:11:14

标签: ios objective-c facebook

我使用facebook SDK进行身份验证。如果在设置中没有启用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];
    }];

否则,如果启用了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];
            }
        }];
    }

enter image description here}

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

感谢所有人。

1 个答案:

答案 0 :(得分:1)

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

将基于备份网络的身份验证移动到其他方法,然后检查您是否可以按原样创建帐户存储。如果没有,请调用Web方法。如果可以,请尝试使用requestAccessToAccountsWithType:options:completion:访问FB帐户。在完成块中,您将被授予访问权限,或者您将调用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];
                                        }
                                        });
                                    }];
}
相关问题