iOS Facebook Framework login process

六眼飞鱼酱① 提交于 2019-12-12 04:18:50

问题


I am developing an iOS app in which I am providing functionality to login via Facebook. For this I am using the Facebook framework. I have gone through developers.facebook.com and implemented login functionality as described there. FB login is working properly. FB login functionality works like this:

  1. FB Framework will try to find FB App, if it is present then it will authenticate using FB app credentials.

  2. If FB app is not present then it will try to authenticate using the iPhone device settings FB credentials.

  3. If FB System Account is not present then it will open safari for authentication.

I want that in the third step instead of safari Facebook Web view pops up. Below is my code which I am using.

    #import "AppDelegate.h"

    NSString *const FBSessionStateChangedNotification =
    @"com.sampleapp.facebook:FBSessionStateChangedNotification";
    @implementation AppDelegate
    @synthesize 

loggedInUserID = _loggedInUserID, loggedInSession = _loggedInSession;
static NSString* kAppId = **FBAPPID**;

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

    self.loginViewControlObj = [[LoginViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.loginViewControlObj];

    self.navigationController = nav;
    self.navigationController.navigationBarHidden = YES;
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [FBAppCall handleDidBecomeActive];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
     [FBSession.activeSession close];
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
}


#pragma mark - FB Methods

/*
 * Callback for session changes.
 */
- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                //NSLog(@"User session found");
                [FBRequestConnection
                 startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                                   NSDictionary<FBGraphUser> *user,
                                                   NSError *error) {
                     if (!error) {
                         self.loggedInUserID = user.id;
                         self.loggedInSession = FBSession.activeSession;
                     }
                 }];
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }

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

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

/*
 * Opens a Facebook session and optionally shows the login UX.
 */
- (void)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    // Initialize a session object
    FBSession *session = [[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:@"basic_info", @"email", @"publish_stream", @"publish_actions", @"read_friendlists", nil]];
    // Set the active session
    [FBSession setActiveSession:session];
    // Open the session
    [session openWithBehavior:FBSessionLoginBehaviorWithFallbackToWebView
            completionHandler:^(FBSession *session,
                                FBSessionState status,
                                NSError *error) {
                [self sessionStateChanged:session
                                    state:status
                                    error:error];
            }];

}


/*
 *
 */
- (void) closeSession {
    [FBSession.activeSession closeAndClearTokenInformation];
}


@end

I am calling login from another controller and if I don't have the FB app and system account in the device then it opens safari instead of web view.

Please help me solve this if possible.

来源:https://stackoverflow.com/questions/20758220/ios-facebook-framework-login-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!