Facebook iOS SDK登录本机和UIWebView登录 [英] Facebook iOS SDK login native and UIWebView login
问题描述
对于我正在开发的应用程序,我需要用户能够使用本机SDK登录Facebook,但是在webview中使用FB评论小部件的应用程序中还有一个单独的部分。问题是在用户使用本机SDK登录后,他们未在webview评论小部件中登录。有没有办法让用户使用本机iOS SDK登录,然后在UIWebView中将它们登录到Facebook。我尝试在用户登录后在FBSession类中使用openAccessTokenFromData:completionHandler:但无法使其工作,如下所示
For an app I'm working on I need the users to be able to login to Facebook using the native SDK, but there is also a separate part of the app using an FB comments widget in a webview. The problem is after the user logs in using native SDK they are not logged in within the webview comments widget. Is there a way to have the user login using the native iOS SDK and then also log them into Facebook in a UIWebView. I tried using openAccessTokenFromData:completionHandler: in the FBSession class after the user signed in but couldn't get that to work, like below
- (void)didLogin
{
FBAccessTokenData *data = [FBAccessTokenData createTokenFromString:[FBSession activeSession].accessTokenData.accessToken
permissions:[FBSession activeSession].accessTokenData.permissions
expirationDate:[FBSession activeSession].accessTokenData.expirationDate
loginType:FBSessionLoginTypeWebView
refreshDate:nil];
[[FBSession activeSession] closeAndClearTokenInformation];
FBSession *session = [[FBSession alloc] init];
[session openFromAccessTokenData:data
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
}];
}
推荐答案
我用以下代码打开Facebook iOs SDK在我的原生应用程序中登录Webview,它的工作正常。
I used following code to open Facebook iOs SDK login in Webview in my native app and its works fine for me.
-(void)openFacebookAuthentication
{
NSArray *permission = [NSArray arrayWithObjects:kFBEmailPermission,kFBUserPhotosPermission, nil];
FBSession *session = [[FBSession alloc] initWithPermissions:permission];
[FBSession setActiveSession: [[FBSession alloc] initWithPermissions:permission] ];
[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
switch (status) {
case FBSessionStateOpen:
[self getMyData];
break;
case FBSessionStateClosedLoginFailed: {
// prefer to keep decls near to their use
// unpack the error code and reason in order to compute cancel bool
NSString *errorCode = [[error userInfo] objectForKey:FBErrorLoginFailedOriginalErrorCode];
NSString *errorReason = [[error userInfo] objectForKey:FBErrorLoginFailedReason];
BOOL userDidCancel = !errorCode && (!errorReason || [errorReason isEqualToString:FBErrorLoginFailedReasonInlineCancelledValue]);
if(error.code == 2 && ![errorReason isEqualToString:@"com.facebook.sdk:UserLoginCancelled"]) {
UIAlertView *errorMessage = [[UIAlertView alloc] initWithTitle:kFBAlertTitle
message:kFBAuthenticationErrorMessage
delegate:nil
cancelButtonTitle:kOk
otherButtonTitles:nil];
[errorMessage performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
errorMessage = nil;
}
}
break;
// presently extension, log-out and invalidation are being implemented in the Facebook class
default:
break; // so we do nothing in response to those state transitions
}
}];
permission = nil;
}
这篇关于Facebook iOS SDK登录本机和UIWebView登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!