简体   繁体   中英

Facebook SDK 3.1 for iOS login issues

I have used Facebook sdk 3.1 in my ios app for sharing link on friends wall. I try to open a new session in the applicationDidFinishLaunching method as below

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

myAppDelegate = self;

AudioViewController *viewController = [[AudioViewController alloc] init];
self.navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.navController setNavigationBarHidden:YES];
viewController = nil;


self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];

if (![self openSessionWithAllowLoginUI:NO]) {
    // No? Display the login page.
    [self performSelector:@selector(login) withObject:nil afterDelay:0.5];
}

return YES;
}

- (void)login{
[self openSessionWithAllowLoginUI:YES];
}

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
return [FBSession openActiveSessionWithReadPermissions:nil
                                          allowLoginUI:allowLoginUI
                                     completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                         [self sessionStateChanged:session state:state error:error];
                                     }];
}

- (void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState)state
                  error:(NSError *)error{

switch (state) {
    case FBSessionStateOpen: {
        DLOG(@"session open");
    }
        break;
    case FBSessionStateClosed: {

        DLOG(@"session closed");


        [FBSession.activeSession closeAndClearTokenInformation];


    }
        break;
    case FBSessionStateClosedLoginFailed: {


         DLOG(@"session Failed");


    }
        break;
    default:
        break;
}

[[NSNotificationCenter defaultCenter] postNotificationName:WVSessionStateChangedNotification
                                                    object:session];

if (error) {
    DLOG(@"error = %@",error);
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Error: %@",
                                                                 [AppDelegate FBErrorCodeDescription:error.code]]
                                                        message:error.localizedDescription
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [alertView show];
}
}

With some of the Facebook accounts it returns error "The operation could not be completed" com.facebook.sdk error 2. and so further cannot post on Facebook.

Am I doing something wrong here ?Any help would be appreciated.!

I had same issue, and i have solved it by calling openSessionWithAllowLoginUI:TRUE via NSTimer.

if (![self openSessionWithAllowLoginUI:NO]) {
    // No? Display the login page.
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(login) userInfo:nil repeats:NO];
}

The reason is, we can't get FB session on different threads, either we use main thread or another(background) thread. In your code, when you check session availability, you use main thread and for login, you use different thread via performSelector function.

Hope, it will help you.

thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM