如何在“允许这些应用使用您的帐户”的条件下解决“com.facebook.sdk错误2”对我的应用已关闭

时间:2013-04-15 01:46:22

标签: ios facebook login

我正在使用最新的FB SDK进行本地登录的iOS应用程序。当我在设置中“允许这些应用程序使用您的帐户”关闭我的应用程序时,出现错误“com.facebook.sdk error 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];}];

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

1 个答案:

答案 0 :(得分:0)

这就是我解决它的方式。在AppDelegate实现文件的applicationDidBecomeActive方法中,按照FB SDK文档的建议使用常规[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 + Storyboards with Publish to Feed

我希望它有所帮助。