【问题标题】:ACAccountType always showing 0 accountsACAccountType 始终显示 0 个帐户
【发布时间】:2013-06-18 07:11:13
【问题描述】:

我已使用 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];
    }];

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

}

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

谢谢大家。

【问题讨论】:

  • 你给requestAccessToAccountsWithType:options:completion:打过电话吗?
  • @Wain 我应该在哪里称呼它?

标签: ios objective-c facebook


【解决方案1】:

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

将您的备份基于 Web 的身份验证转换为其他方法,然后检查您是否可以按原样创建帐户存储。如果没有,请调用 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];
                                        }
                                        });
                                    }];
}

【讨论】:

  • 显示此错误 -[FBSession checkThreadAffinity] NSInternalInconsistencyException',原因:'FBSession: 只能在单线程中使用'
  • 显示此错误 -[FBSession checkThreadAffinity] NSInternalInconsistencyException',原因:'FBSession: 只能在单线程中使用'
  • 您还尝试在哪里与 FB 交互以及在哪个线程上进行交互?从技术上讲,在完成块中,您应该切换回主线程以确保安全。
  • 如果你能把代码上传到github/粘贴/东西我可以稍后看看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多