如何加载Facebook网址(例如Facebook Mobile)作为已经过身份验证的用户?
使用Facebook SDK并已使用SSO登录,当我尝试在UIwebView中加载facebook URL时,我得到一个“你必须先登录”..我猜是因为来自实际Safari的不同cookie浏览器。
答案 0 :(得分:1)
似乎不可能,因为您已在该特定应用程序的UIWebView中登录到FB,但它未在所有将来的会话中保存凭据,因此,它仅使用一次,然后下次重新分配UIWEBVIEW时相同的应用程序,以前的所有会话都已消失,您必须验证用户访问Facebook个人资料。
答案 1 :(得分:0)
首先,您需要在身份验证期间更改下面的行为。
[FBSession setActiveSession:sess]; [sess openWithBehavior:(FBSessionLoginBehaviorForcingWebView)completionHandler:^(FBSession session,FBSessionState state,NSError error) {}];
它肯定会起作用。
答案 2 :(得分:0)
//首先,您需要使用图形api对Facebook进行身份验证,例如下面的内容。 set authentication总是使用" 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];
}