【问题标题】:How will Access a public Facebook profile in UIWebView如何在 UIWebView 中访问公共 Facebook 个人资料
【发布时间】:2015-05-19 11:44:19
【问题描述】:

如何以已通过身份验证的用户身份加载 Facebook URL(例如 Facebook Mobile)?

使用 Facebook SDK 并已使用 SSO 登录,当我尝试在 UIwebView 中加载 facebook URL 时,我收到“您必须先登录”。我猜测是由于实际 Safari 的 cookie 不同浏览器。

【问题讨论】:

    标签: ios facebook facebook-graph-api cookies uiwebview


    【解决方案1】:

    这似乎是不可能的,因为您已经在该特定应用程序的 UIWebView 中登录到 FB,但它并没有在未来的所有会话中保存凭据,因此,当您在同一个应用程序,之前的所有会话都消失了,您必须对用户进行身份验证才能访问 Facebook 个人资料。

    【讨论】:

    【解决方案2】:

    首先,您需要在身份验证期间更改下面的行为。

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

    它肯定会起作用。

    【讨论】:

      【解决方案3】:

      // 首先,您需要使用图形 api 向 facebook 进行身份验证,例如下面。设置身份验证始终使用“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];
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多