0

我正在使用最新的 FB SDK 进行本机登录的 iOS 应用程序。当我在设置中的“允许这些应用程序使用您的帐户”中关闭我的应用程序时,预计会出现错误“com.facebook.sdk 错误 2”来。

我想知道即使“允许这些应用程序使用您的帐户”对我的应用程序关闭,是否有任何优雅的方法可以解决此错误?我已经搜索了解决方案,但所有答案都说您需要打开该选项。但我认为更好的方法是,如果用户关闭该选项,我们仍然可以让他登录,无缝回退到快速应用切换方式,就像他根本没有在他的设备上登录 Facebook 一样。如何在最新的 FB SDK 中做到这一点?谢谢!

=====================================更新============ ============================ 我使用已弃用的函数 openActiveSessionWithPermissions:allowLoginUI:completionHandler 解决它

首先我们需要检查用户是否关闭了这个选项:

    self.useAccountAllowed = true;
    ACAccountStore *accountStore;
    ACAccountType *accountTypeFB;
    if ((accountStore = [[ACAccountStore alloc] init]) &&
        (accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook] ) ){

        NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB];
        id account;
        if (!fbAccounts)
        {
            //do not log into FB on the device
        }
        else if ([fbAccounts count] == 0) {
            [FBSession.activeSession closeAndClearTokenInformation];
            self.useAccountAllowed = false;  //user switch this option off
        } 

然后在 openSession 函数中,如果 self.useAccountAllowed 为 false,则使用已弃用的函数:

if (self.useAccountAllowed) {
        [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession* session, FBSessionState status, NSError* error){
            [self sessionStateChanged:session state:status error:error];}];
    }
    else {
        NSArray* lPermission = FBSession.activeSession.permissions;
        [FBSession openActiveSessionWithPermissions:lPermission allowLoginUI:YES completionHandler:^(FBSession* session, FBSessionState status, NSError* error){
            [self sessionStateChanged:session state:status error:error];}];

不确定这是否是正确的方法。

4

1 回答 1

0

这就是我解决它的方法。在 AppDelegate 实现文件的方法中,使用FB SDK 文档推荐applicationDidBecomeActive的常规方法。另外,在设置中添加一个检查用户权限的新方法(我在下面的示例中调用了该方法):[FBSession.activeSession handleDidBecomeActive]checkPermissionSettings

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"applicationDidBecomeActive: in NHOCAppDelegate");
    //
    // The flow back to your app may be interrupted (for ex: if the user clicks the Home button
    // while if authenticating via the Facebook for iOS app).
    // If this happens, the Facebook SDK can take care of any cleanup that may include starting a fresh session.
    //
    [FBSession.activeSession handleDidBecomeActive];
    [self checkPermissionSettings];
}

//
// Verify if the user pressed the Home Button, went to Settings and deauthorized the app via "Allow These Apps to Use Your Account..."
// If so, redirect him to the login screen (this happens automagically, see below).
//
- (void)checkPermissionSettings
{
    NSLog(@"checkPermissionSettings: in NHOCAppDelegate");
    //
    // Now 'startForMeWithCompletionHandler' may return 'FBSessionStateClosed' (meaning that the user probably unauthorized the app in Settings).
    //
    // If that is the case:
    //
    //  - Hide the 'logged' View Controller
    //  - Remove it (NHOCLoggedVC) from the Notification Center
    //  - Show the 'login' View Controller
    //  - And finally add it (NHOCLoginVC) to the Notification Center, closing the loop
    //
    // Check the console for further info.
    //
    [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id<FBGraphUser> user, NSError *error) {

        if (!error) {
            //
            // Everything went fine... The app is in good shape.
            // Notice that 'user.location' requires user_location permission
            //
            NSLog(@"user.location: %@: ", [user.location objectForKey:@"name"]);
        }
    }];
}

为了使其按设计工作,我还使用通知中心。您可以在此处查看整个示例:

FB SDK + 带有发布到 Feed 的情节提要

我希望它有所帮助。

于 2013-04-17T04:18:44.430 回答