将用户的 Facebook 凭据导入 iOS6



新的Facebook SDK 3.0的文档告诉我们:

"在某些情况下,iOS设备不会为已经将应用程序连接到其Facebook帐户的用户提供访问令牌。当用户使用应用程序的网站或在另一台移动设备上使用该应用程序时,可能会发生这种情况。要使用iOS 6本机身份验证对话框,应用程序需要将用户的凭据‘导入’到设备中,并需要显示iOS 6本地身份验证对话框。">

(参见http://developers.facebook.com/docs/howtos/ios-6/,提示2)

我想知道如何导入此数据,我找不到代码示例。

谢谢,

Tim

附言:出于信息(和测试目的),在应用程序中复制上述情况很容易:

  1. 在设置应用程序中,删除Facebook帐户
  2. 在开发中的应用程序中,通过SSO连接到Facebook,(考虑到没有在iOS6下注册的Facebook帐户,应该回到这一点
  3. 在设置应用程序中重新设置Facebook帐户
  4. 再次尝试通过正在开发的应用程序登录。这一次,它将仍然通过SSO访问Facebook,因为令牌是使用它创建的

正如NickBarker先生所建议的,解决方案是将用户注销SSO。然而,我只想这样做,前提是我绝对确信这样做时,他们的用户将通过iOS 6身份验证系统重新登录。

我的问题是,我不知道如何确保用户通过"设置"应用程序登录Facebook,我也不想让用户从Facebook注销,只是让他们每次都发送回Facebook应用程序进行SSO登录。

ACAccountStore的任何方法都不会告诉你用户是否在设置应用程序中输入了他们的Facebook详细信息,无论我最终发现[SLComposeViewController是可用的ForServiceType: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];
}

在某些情况下,iOS设备不会为已经将应用程序连接到其Facebook帐户的用户提供访问令牌。当用户使用应用程序的网站或在其他移动设备上使用该应用程序时,可能会发生这种情况。要使用iOS 6原生身份验证对话框,应用程序需要将用户的凭据"导入"到设备中,并需要显示iOS 6原生的身份验证对话框一个很好的方法是请求基本配置文件信息-请参阅上面的步骤1

注意粗体文本,只需使用基本前提条件请求访问,即openActiveSessionWithPremisions:。。。并且会话将持有访问令牌。

如果它仍然使用SSO,请首先关闭活动会话上的AndClearTokenInformation。

虽然迂回接受的答案可能有效,但该功能已内置于Facebook SDK中。您要查找的是FBSession openWithBehavior:completionHandler:方法,并指定FBSessionLoginBehaviorUseSystemAccountIfPresent的行为。它需要一些额外的工作,因为您不能使用方便类方法FBSession openActiveSessionWithReadPermissions:allowLoginUI:completionHandler:。但是初始化会话然后设置活动会话的额外工作是微不足道的。

最新更新