新Facebook SDK 3.0的文档告诉我们:
“在某些情况下,iOS设备不会为已经将应用程序连接到其Facebook帐户的用户提供访问令牌。这可能发生在用户使用应用程序的网站或在其他移动设备上使用该应用程序时要使用iOS 6原生Auth Dialog,应用程序需要将用户的凭据“导入”到设备中,并需要显示iOS 6原生Auth对话框。“
(见http://developers.facebook.com/docs/howtos/ios-6/,提示2)
我想知道如何导入这些数据,我找不到代码示例。
谢谢,
添
P.S。有关信息(和测试目的),很容易在应用程序中重现上述情况:
答案 0 :(得分:2)
MrNickBarker建议解决方案是将用户从SSO中删除。但是,我只想这样做,如果我完全确定这样做,用户将通过iOS 6身份验证系统重新登录。
我的问题是我不知道确保用户通过“设置”应用登录Facebook的方法,我不想让用户退出Facebook只是让他们发回给Facebook应用程序每次都进行SSO登录。
ACAccountStore方法都不会告诉您用户是否已将他们的Facebook详细信息输入到“设置”应用中,无论我最终发现[SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]都是这样做的。
这是解决方案:
ACAccountStore *accountStore = [[NSClassFromString(@"ACAccountStore") alloc] init];
ACAccountType *accountType;
if (accountStore &&
(accountType = [accountStore accountTypeWithAccountTypeIdentifier:@"com.apple.facebook"]) &&
[SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
// There's an account store and it knows about Facebook - we must be on iOS 6 or higher.
//
// [SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook] also tells us that
// the user has signed into their Faecbook account via the Settings app.
//
// At this point there could be a valid session that's been previously created via SSO prior to the
// user signing into Facebook from Settings.app. In this case, calling openActiveSessionWithReadPermissions:
// will continue to log on using SSO rather than the preferred route of using the built in iOS support.
//
// [accountStore accountsWithAccountType:accountType] will return an account for this application once
// it's been used to log into Facebook. Clearly, if there is an account returned then the then we don't want to
// force a logout.
//
// On the other hand, if no accounts are returned then we can safely call closeAndClearTokenInformation
// in order to clear any SSO tokens, thereby ensuring that the next login will attempt to use the iOS
// authentication (which we can do since the user has signed into the Settings app).
if ([[accountStore accountsWithAccountType:accountType] count] == 0)
[FBSession.activeSession closeAndClearTokenInformation];
// Try to connect with read permissions only (as required for iOS auth)
_usingiOSIntegratedLogin = YES;
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:completionHandler];
}
else
{
// Either this is < iOS 6, or there's no Facebook account registered - request both
// read and write permissions in order to avoid two trips to the Facebook app or web view
_usingiOSIntegratedLogin = NO;
NSMutableArray *permissions = [NSMutableArray arrayWithArray:[self publishPermissions]];
[permissions insertObject:@"user_photos" atIndex:0];
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:completionHandler];
}
答案 1 :(得分:0)
在某些情况下,iOS设备将没有已将应用程序连接到其Facebook帐户的用户的访问令牌。当用户使用应用程序的网站或在其他移动设备上使用该应用程序时,可能会发生这种情况。要使用iOS 6原生Auth Dialog,应用程序需要将用户的凭据“导入”到设备中,并需要显示iOS 6本机Auth Dialog。 执行此操作的一种好方法是请求基本个人资料信息 - 请参阅上面的步骤1.
注意粗体文本,只需要使用基本提交请求访问权限,即openActiveSessionWithPremissions:...并且会话将保留访问令牌。
如果它仍然使用SSO,请首先在活动会话上关闭AndClearTokenInformation。
答案 2 :(得分:0)
虽然环形交叉口接受的答案可能有效,但此功能内置于Facebook SDK中。您要查找的是FBSession openWithBehavior:completionHandler:
方法并指定FBSessionLoginBehaviorUseSystemAccountIfPresent
的行为。它需要一些额外的工作,因为你不能使用方便类方法FBSession openActiveSessionWithReadPermissions:allowLoginUI:completionHandler:
。但是初始化会话然后设置活动会话的额外工作是微不足道的。