如何在 UIWebView 中访问 public Facebook 个人资料

How will Access a public Facebook profile in UIWebView

如何加载 Facebook URLs(例如 Facebook Mobile)作为已经通过身份验证的用户?

使用 Facebook SDK 并且已经使用 SSO 登录,当我尝试在 UIwebView 中加载 facebook URL 时,我得到一个 "You must log in first"..我猜是由于不同的 cookie来自实际的 Safari 浏览器。

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

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

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

它一定会起作用。

// 首先,您需要使用图 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];
}