iOS Facebook Framework登录过程

时间:2013-12-24 09:25:45

标签: ios iphone facebook-graph-api facebook-ios-sdk social-framework

我正在开发一个iOS应用程序,我在其中提供通过Facebook登录的功能。为此我使用的是Facebook框架。我已经浏览了developers.facebook.com并实现了那里描述的登录功能。 FB登录正常。 FB登录功能的工作方式如下:

  1. FB Framework将尝试查找FB App,如果存在,则会使用FB应用凭据进行身份验证。

  2. 如果FB应用程序不存在,则会尝试使用iPhone设备设置FB凭据进行身份验证。

  3. 如果没有FB系统帐户,则会打开safari进行身份验证。

  4. 我希望在第三步而不是safari Facebook Web视图弹出。以下是我正在使用的代码。

        #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
    

    我正在从另一个控制器调用登录,如果我在设备中没有FB应用程序和系统帐户,则会打开safari而不是Web视图。

    请尽可能帮我解决。

0 个答案:

没有答案