本文介绍了如何解决“com.facebook.sdk错误2”在“允许这些应用使用您的帐户”的条件下是我的应用程序关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an iOS app using the latest FB SDK for native log in. When I switch my app off in "allow these apps to use your account" in the settings, an error "com.facebook.sdk error 2" is expected to come.

我想知道有没有任何优雅的方式来解决这个错误,即使允许这些应用使用您的帐户是我的应用程序?我已经搜索了解决方案,但所有的答案都表示您需要切换该选项。但是我认为更好的方法是,如果用户切换该选项,我们仍然可以让他登录,无缝地回到快速应用切换方式,就像他没有在他的设备上登录Facebook一样。如何在最新的FB SDK中执行此操作?谢谢!

I am wondering is there any elegant way to solve this error even if "allow these apps to use your account" is off for my app? I have searched for the solution but all the answers are saying that You need to switch that option on. But I think the better way is that if user switches that option off, we can still let him log in, falling back to the fast-app-switch way seamlessly, just like he doesn't log into Facebook on his device at all. How can I do this in the newest FB SDK? Thanks!

============================= ===更新=======================================
I解决它使用不推荐的功能openActiveSessionWithPermissions:allowLoginUI:completionHandler

====================================Update=========================================I solve it using a deprecated function openActiveSessionWithPermissions:allowLoginUI:completionHandler

首先我们需要检查用户是否切换此选项:

first we need to check whether user switch this option off:

    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:

then in openSession function, using that deprecated function if self.useAccountAllowed is 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];}];

不知道是否是正确的方式。

not sure whether it is a correct way.

推荐答案

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

This is how I solved it. On the AppDelegate implementation file, in the applicationDidBecomeActive method, use the regular [FBSession.activeSession handleDidBecomeActive] method, as recommended by the FB SDK documentation. Plus, add a new method that checks the user permissions in Settings (that I called checkPermissionSettings in the example below):

- (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"]);
        }
    }];
}

为了使其按照设计工作,我还使用通知中心。你可以在这里查看整个例子:

To make it work as designed, I also use Notification Center. You can check the entire example here:

我希望它有帮助。

这篇关于如何解决“com.facebook.sdk错误2”在“允许这些应用使用您的帐户”的条件下是我的应用程序关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 00:42