2013-04-18 49 views
0

使用FacebookSDK登錄Facebook後,我想使用UIWebView顯示一個頁面。 第一次登錄時顯示正確。 但我關閉並重新啓動應用程序,即使facebook會話仍然打開,我無法登錄到我的UIWebView會話。如何通過FacebookSDK獲取會話登錄信息到我的UIWebView

當我重新運行應用程序以獲取Facebook會話信息時,您是否有任何建議?

這是我的代碼示例。

@interface FBLogin2ViewController() 

@end 

@implementation FBLogin2ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 

    if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) { 
     // To-do, show logged in view 

     NSString *appID = [FBSession defaultAppID]; 

     NSLog(@"Session token exists %@", appID); 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)onLogin:(id)sender { 

    NSArray *permissions = nil; 

    if (![FBSession activeSession]) { 
     FBSession *session = [[FBSession alloc] initWithAppID:@"000000000000" permissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone urlSchemeSuffix:@"fb" tokenCacheStrategy:nil]; 

     [FBSession setActiveSession:session]; 
    } 
    else if([FBSession activeSession].isOpen) 
    { 
     [[FBSession activeSession] closeAndClearTokenInformation]; 
     FBSession.activeSession = nil; 
    } 

    [[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView 
           completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { 
              [self sessionStateChanged:session state:state error:error]; 
             }]; 

} 

- (IBAction)onLogout:(id)sender { 

    [FBSession.activeSession closeAndClearTokenInformation]; 
    FBSession.activeSession = nil; 
} 

- (IBAction)onGoWeb:(id)sender { 
    NSURL *url = [NSURL URLWithString:@"http://www.facebook.com/xxxxx?fref=xx"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [_webView loadRequest:request]; 
    _webView.delegate = self; 
} 


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

     } 
      break; 
     case FBSessionStateClosed: 
     case FBSessionStateClosedLoginFailed: 
      // Once the user has logged in, we want them to 
      // be looking at the root view. 


      break; 
     default: 
      break; 
    } 

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

    @end 

回答

0

1)調用openFBSessionIfAvaliable在didFinishLaunchingWithOptions方法

//in appdelegate 
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
     { 
      [self openFBSessionIfAvaliable]; 
     } 

//in appdelegate 
    - (BOOL)application:(UIApplication *)application 
       openURL:(NSURL *)url 
     sourceApplication:(NSString *)sourceApplication 
      annotation:(id)annotation { 
     // attempt to extract a token from the url 
     return [[FBSession activeSession] handleOpenURL:url]; 
    } 
//in appdelegate 
    - (void)applicationDidBecomeActive:(UIApplication *)application 
    { 
     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
     [[FBSession activeSession] handleDidBecomeActive]; 
    } 

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

-(void)openFBSessionIfAvaliable 
{ 
    [FBSession setActiveSession:[FBSession new]]; 
    if ([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded) 
    { 
     [self openFBSessionWithAllowLoginUI:NO]; 
    } 
} 

- (BOOL)openFBSessionWithAllowLoginUI:(BOOL)allowLoginUI 
{ 
    NSArray *permissions = [[NSArray alloc] initWithObjects: 
          @"email",nil]; 
    return [FBSession openActiveSessionWithReadPermissions:permissions 
               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: 
      if (!error) 
      { 
       NSString *_facebookToken = [[FBSession activeSession] accessToken]; 
       NSString *_facebookEmail = [user objectForKey:@"email"]; 
      } 
      break; 
     case FBSessionStateClosed: 
     case FBSessionStateClosedLoginFailed: 
      [FBSession.activeSession closeAndClearTokenInformation]; 
      break; 
     default: 
      break; 
    } 

    if (error) 
    { 
     [FBSession.activeSession closeAndClearTokenInformation]; 
     /*UIAlertView *alertView = [[UIAlertView alloc] 
     initWithTitle:@"Error" 
     message:@"Please try Again" 
     delegate:nil 
     cancelButtonTitle:@"OK" 
     otherButtonTitles:nil]; 
     [alertView show];*/ 

    } 

} 

2)點擊 「登錄通過臉譜」 呼叫以下方法

[self openFBSessionWithAllowLoginUI:YES]; 

3)現在,每次當你啓動時間該應用程序,它會檢查Facebook會話,如果發現然後進一步進行,否則顯示按鈕「通過Facebook登錄」.session

我希望它能解決你的問題。