正如 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];
}