Facebook iOS SDK login native and UIWebView login

前端 未结 3 2245
别跟我提以往
别跟我提以往 2020-12-15 01:17

For an app I\'m working on I need the users to be able to login to Facebook using the native SDK, but there is also a separate part of the app using an FB comments widget in

3条回答
  •  一生所求
    2020-12-15 01:47

    I used following code to open Facebook iOs SDK login in Webview in my native app and its works fine for me.

    -(void)openFacebookAuthentication
    {
        NSArray *permission = [NSArray arrayWithObjects:kFBEmailPermission,kFBUserPhotosPermission, nil];
    
        FBSession *session = [[FBSession alloc] initWithPermissions:permission];
    
        [FBSession setActiveSession: [[FBSession alloc] initWithPermissions:permission] ];
    
        [[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
    
            switch (status) {
                case FBSessionStateOpen:
                    [self getMyData];
                    break;
                case FBSessionStateClosedLoginFailed: {
                    // prefer to keep decls near to their use
                    // unpack the error code and reason in order to compute cancel bool
                    NSString *errorCode = [[error userInfo] objectForKey:FBErrorLoginFailedOriginalErrorCode];
                    NSString *errorReason = [[error userInfo] objectForKey:FBErrorLoginFailedReason];
                    BOOL userDidCancel = !errorCode && (!errorReason || [errorReason isEqualToString:FBErrorLoginFailedReasonInlineCancelledValue]);
    
    
                    if(error.code == 2 && ![errorReason isEqualToString:@"com.facebook.sdk:UserLoginCancelled"]) {
                        UIAlertView *errorMessage = [[UIAlertView alloc] initWithTitle:kFBAlertTitle
                                                                               message:kFBAuthenticationErrorMessage
                                                                               delegate:nil
                                                                               cancelButtonTitle:kOk
                                                                               otherButtonTitles:nil];
                        [errorMessage performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
                        errorMessage = nil;
                        }
                    }
                    break;
                    // presently extension, log-out and invalidation are being implemented in the Facebook class
                default:
                    break; // so we do nothing in response to those state transitions
            }
        }];
        permission = nil;
    }
    

提交回复
热议问题