将用户的Facebook凭据导入iOS6

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

“在某些情况下,iOS设备对于已经将应用程序连接到他们的Facebook帐户的用户将没有访问令牌。当用户使用应用程序的网站或在另一个移动设备上使用该应用程序时,可能会发生这种情况。 iOS 6本地身份validation对话框,应用程序需要将用户凭据“导入”设备,并需要显示iOS 6本机身份validation对话框。

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

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

谢谢,

蒂姆

PS有关信息(和testing目的),很容易在应用程序中重现上述情况:

  1. 在设置应用程序中,删除Facebook帐户
  2. 在开发的应用程序通过SSO连接到Facebook,(它应该回落到这个给定的是没有Facebook帐户注册在iOS6下
  3. 在设置应用程序中再次设置Facebook帐户
  4. 再次尝试通过开发中的应用程序login。 这次它将通过SSO访问Facebook,因为令牌是使用它创build的。

正如MrNickBarkerbuild议解决scheme是将用户登出SSO。 不过,我只是想这样做,如果我绝对确信这样做,他们将通过iOS 6身份validation系统重新login。

我的问题是,我不知道确保用户通过设置应用程序login到Facebook的方式,我不想只将用户登出Facebook,让他们发回Facebook应用程序每次都有一个SSOlogin。

没有任何一个ACAccountStore方法会告诉你用户是否已经将他们的Facebook详细信息input到设置应用程序中,但是我终于发现了[SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]。

这里是解决scheme:

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本地身份validation对话框,应用程序需要将用户的凭据“导入”设备,并需要显示iOS 6本机身份validation对话框。 一个很好的方法是请求基本的configuration文件信息 – 参见上面的步骤1。

注意粗体文本,只是请求访问与基本的预备,即openActiveSessionWithPremissions:…会话将持有访问令牌。

如果它仍然使用SSO,请在活动会话上先closures并清除TokenInformation。

尽pipe迂回接受的答案可能有效,但是这个function已经内置到了Facebook SDK中。 你正在寻找的是FBSession openWithBehavior:completionHandler:方法,并指定FBSessionLoginBehaviorUseSystemAccountIfPresent的行为。 它需要一点额外的工作,因为你不能使用便利的类方法FBSession openActiveSessionWithReadPermissions:allowLoginUI:completionHandler: 但是额外的工作来初始化会话,然后设置活动会话是微不足道的。