2015-05-19 80 views

回答

0

//首先,您需要使用图表api进行身份验证,例如下图。一套身份验证始终使用“FBSessionLoginBehaviorForcingWebView”

- (IBAction)clkFacebook:(id)sender { 
    if (![SmashboardAPI hasInternetConnection]) { 
     [AppDelegate showAlert:@"Error" message:@"Please check your internet connection."]; 
    } else { 
     //[self disabledAllSocialButtons]; 
     [self showSpinner]; 
     // If the session state is any of the two "open" states when the button is clicked 
     if (FBSession.activeSession.state == FBSessionStateOpen 
      || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) { 

      // Close the session and remove the access token from the cache 
      // The session state handler (in the app delegate) will be called automatically 
      // If the session state is not any of the two "open" states when the button is clicked 
     } 
     else 
     { 

      FBSession* sess = [[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:@"public_profile",@"user_friends",@"email",nil]]; 

      [FBSession setActiveSession:sess]; 
      [sess openWithBehavior:(FBSessionLoginBehaviorForcingWebView) completionHandler:^(FBSession *session, FBSessionState state, NSError *error) 
      { 
       [appDel sessionStateChanged:session state:state error:error]; 

       [self showSpinner]; 
       [self getLoggedFBUserDetails]; 
      }]; 
     } 
    } 
} 

//然后在Appdelegate.m使用此方法的话,你可以在应用程序的任何地方访问。

- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error 
{ 
    // If the session was opened successfully 
    if (!error && state == FBSessionStateOpen){ 
     // NSLog(@"Session opened"); 
     // Show the user the logged-in UI 
     self.fbSession = FBSession.activeSession; 
     //[self userLoggedIn]; 
     return; 
    } 
    if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed) { 
     // If the session is closed 
     // NSLog(@"Session closed"); 
     // Show the user the logged-out UI 
     [self userLoggedOut]; 
     return; 
    } 

    // Handle errors 
    if (error){ 
     // NSLog(@"Error"); 
     NSString *alertText; 
     NSString *alertTitle; 
     // If the error requires people using an app to make an action outside of the app in order to recover 
     if ([FBErrorUtility shouldNotifyUserForError:error] == YES){ 
      alertTitle = @"Something went wrong"; 
      alertText = [FBErrorUtility userMessageForError:error]; 
      [self showMessage:alertText withTitle:alertTitle]; 
     } else { 

      // If the user cancelled login, do nothing 
      if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) { 
       // NSLog(@"User cancelled login"); 

       // Handle session closures that happen outside of the app 
      } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){ 
       alertTitle = @"Session Error"; 
       alertText = @"Your current session is no longer valid. Please log in again."; 
       [self showMessage:alertText withTitle:alertTitle]; 

       // For simplicity, here we just show a generic message for all other errors 
       // You can learn how to handle other errors using our guide: https://developers.facebook.com/docs/ios/errors 
      } else { 
       //Get more error information from the error 
       NSDictionary *errorInformation = [[[error.userInfo objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"]; 

       // Show the user an error message 
       alertTitle = @"Something went wrong"; 
       alertText = [NSString stringWithFormat:@"Please retry. \n\n If the problem persists contact us and mention this error code: %@", [errorInformation objectForKey:@"message"]]; 
       [self showMessage:alertText withTitle:alertTitle]; 
      } 
     } 
     // Clear this token 
     [FBSession.activeSession closeAndClearTokenInformation]; 
     // Show the user the logged-out UI 
     //[self userLoggedOut]; 
    } 
} 

//获取用户详细信息用户这个方法:

- (void)getLoggedFBUserDetails 
{ 
    if (FBSession.activeSession.state == FBSessionStateOpen 
     || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) { 

     //use this active session 
     [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { 
      if (!error) { 
       NSLog([NSString stringWithFormat:@"user info: %@", result]); 

      } else { 
       // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/ 
        [AppDelegate showAlert:@"Facebook internal error" message:[NSString stringWithFormat:@"Description:%@",[error localizedDescription]]]; 
        [self hideSpinner]; 
      } 
     }]; 
    } 
} 

//对于一个UIWebView使用的装载时间线下面的方法:

- (void)loadFacebookTimeline { 
    NSString *strUrl = [NSString stringWithFormat:@"https://www.facebook.com/profile.php?id=%@",facebookUserId]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]]; 
    // Load URL in UIWebView 
    [webview loadRequest:requestObj]; 
} 
1

看来这是不可能的,因为您已经在该特定应用程序的UIWebView中登录到FB,但它不会在将来的所有会话中保存凭据,因此,它只使用一次,然后下次重新分配UIWEBVIEW相同的应用程序,所有以前的会话都不见了,你必须认证用户才能访问Facebook Profile。

+0

感谢Vizllx。但我的观点是“MyPad”应用程序(https://itunes.apple.com/us/app/mypad-for-facebook-instagram/id412133981?mt=8)如何处理这件事。使用webview登录并在webview中显示个人资料页面。 –

0

首先,您需要在认证过程中将行为更改为下方。

[FBSession setActiveSession:sess]; (FBSessionLoginBehaviorForcingWebView)completionHandler:^(FBSession会话,FBSessionState状态,NSError错误) {}];

它肯定会工作。

+0

谢谢Vineet。这个对我有用。 –

相关问题