如何將Facebook網址(如Facebook Mobile)加載爲已認證的用戶?如何在UIWebView中訪問公開的Facebook個人資料
使用Facebook SDK並已登錄到SSO,當我嘗試在UIwebView中加載Facebook URL時,我得到一個「您必須先登錄」..我猜是因爲來自實際的不同cookie Safari瀏覽器。
如何將Facebook網址(如Facebook Mobile)加載爲已認證的用戶?如何在UIWebView中訪問公開的Facebook個人資料
使用Facebook SDK並已登錄到SSO,當我嘗試在UIwebView中加載Facebook URL時,我得到一個「您必須先登錄」..我猜是因爲來自實際的不同cookie Safari瀏覽器。
//首先,您需要使用圖表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];
}
看來這是不可能的,因爲您已經在該特定應用程序的UIWebView中登錄到FB,但它不會在將來的所有會話中保存憑據,因此,它只使用一次,然後下次重新分配UIWEBVIEW相同的應用程序,所有以前的會話都不見了,你必須認證用戶才能訪問Facebook Profile。
首先,您需要在認證過程中將行爲更改爲下方。
[FBSession setActiveSession:sess]; (FBSessionLoginBehaviorForcingWebView)completionHandler:^(FBSession會話,FBSessionState狀態,NSError錯誤) {}];
它肯定會工作。
謝謝Vineet。這個對我有用。 –
感謝Vizllx。但我的觀點是「MyPad」應用程序(https://itunes.apple.com/us/app/mypad-for-facebook-instagram/id412133981?mt=8)如何處理這件事。使用webview登錄並在webview中顯示個人資料頁面。 –