[英]Facebook login in iOS app for multiple users on one device
My app will be used by multiple users. 我的应用程序将被多个用户使用。
How can I integrate facebook in a way so that the safari doesn't save any user credentials & throw a login page each time some one tries to login. 如何以某种方式集成Facebook,以使Safari在每次有人尝试登录时都不会保存任何用户凭据并抛出登录页面。
I already used FBSessionLoginBehaviorForcingWebView to force a web view login within the app but the problem is, when the first user tries to login to fb via the view presented within the app, the control then goes to safari for authentication & it just saves the creds. 我已经使用FBSessionLoginBehaviorForcingWebView强制在应用程序中进行Web视图登录,但是问题是,当第一个用户尝试通过应用程序中提供的视图登录到fb时,控件随后转到了safari进行身份验证,并且只保存了信誉。
So when other user tries to log in, & enters his credentials in the app local UIWebView, the control again goes to safari, which already has previos creds stored & the new user just cannot authenticate. 因此,当其他用户尝试登录并在应用程序本地UIWebView中输入其凭据时,该控件将再次转到safari(已经存储了previos凭据)且新用户无法进行身份验证。
So basically the first user is permanently logged in. Clearing the cookies for web view doesn't work. 因此,基本上第一个用户是永久登录的。清除Web视图的cookie是行不通的。 I cant clear them for safari.
我无法清除它们以进行野生动物园。
Help Thanks in advance. 帮助预先感谢。
This is what I have been doing 这就是我一直在做的
if([FBSession activeSession].isOpen)
{
[[FBSession activeSession] closeAndClearTokenInformation];//Also, clearing all the local cookies
}
else
{
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
delegate.session = [[FBSession alloc] init];
[FBSession setActiveSession:delegate.session];
[delegate.session openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session1, FBSessionState status, NSError *error) {
if (!error) {
[self openSession]; // your method
}
}];
}
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error{
switch (state) {
case FBSessionStateOpen:
{
[FBRequestConnection startForPostStatusUpdate:@"Some message"completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
//logout again
NSLog(@"startForPostStatusUpdate SUCESS");
}
else {
//logout again
NSLog(@"startForPostStatusUpdate FAIL");
}
}];
}
break;
case FBSessionStateClosed:
{
NSLog(@"FBSessionStateClosed");
}
break;
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
Also, modified the app delegate with your methods. 另外,使用您的方法修改应用程序委托。
Found a solution. 找到了解决方案。 Basically, calling the following:
基本上,调用以下命令:
[FBSession.activeSession closeAndClearTokenInformation];
[FBSession.activeSession close];
[FBSession setActiveSession:nil];
Only clears the local FB session information but not the Safari cookies. 仅清除本地FB会话信息,而不清除Safari cookie。 So, after I log in the user, I clear the Safari cookies:
因此,登录用户后,我将清除Safari cookie:
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];
It isn't very elegant, but it works. 它不是很优雅,但可以。 Now, when the next user tries to log in, it forces them to enter their credentials.
现在,当下一个用户尝试登录时,它将强制他们输入其凭据。
You just need to use this method after you want to close the FB Session. 要关闭FB会话后,只需使用此方法。
[FBSession.activeSession closeAndClearTokenInformation];
This will clear the Facebook Token, and whenever button will be pressed for Facebook share, it will ask for new token credential and it will show Facebook login view again. 这将清除Facebook令牌,每当按下按钮以共享Facebook时,它将要求提供新的令牌凭据,并再次显示Facebook登录视图。
Use following code in AppDelegate 在AppDelegate中使用以下代码
// This method will handle facebook session.
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
// Facebook SDK * login flow *
// Attempt to handle URLs to complete any auth (e.g., SSO) flow.
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication fallbackHandler:^(FBAppCall *call) {
// Facebook SDK * App Linking *
// For simplicity, this sample will ignore the link if the session is already
// open but a more advanced app could support features like user switching.
if (call.accessTokenData) {
if ([FBSession activeSession].isOpen) {
NSLog(@"INFO: Ignoring app link because current session is open.");
}
else {
[self handleAppLink:call.accessTokenData];
}
}
}];
}
// Helper method to wrap logic for handling app links.
- (void)handleAppLink:(FBAccessTokenData *)appLinkToken {
// Initialize a new blank session instance...
FBSession *appLinkSession = [[FBSession alloc] initWithAppID:nil
permissions:nil
defaultAudience:FBSessionDefaultAudienceNone
urlSchemeSuffix:nil
tokenCacheStrategy:[FBSessionTokenCachingStrategy nullCacheInstance] ];
[FBSession setActiveSession:appLinkSession];
// ... and open it from the App Link's Token.
[appLinkSession openFromAccessTokenData:appLinkToken
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
// Forward any errors to the FBLoginView delegate.
if (error) {
//TODO: Show error here
}
}];
}
Add following code where you are opening session for Facebook 在打开Facebook会话的位置添加以下代码
/***********************************************************************
This method opens browser or App Url to FB authentication and change FB
session state
************************************************************************/
- (void)openSession
{
NSArray *permisssions = [[NSArray alloc] initWithObjects:@"email",@"user_photos",nil];
[FBSession openActiveSessionWithReadPermissions:permisssions
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
/***********************************************************************
This method will be called whenever FB Session state changed
It also shows an error message if any error occurs
************************************************************************/
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
NSLog(@"FBSession Open State");
//Enter your code what you want to perform when session is open
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Once the user has logged in, we want them to
// be looking at the root view.
NSLog(@"FBSession State Closed");
[FBSession.activeSession closeAndClearTokenInformation];
[FBSession.activeSession close];
[FBSession setActiveSession:nil];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
Now when ever user tap on Facebook Share button check that session exists or not. 现在,当用户点击Facebook共享按钮时,检查该会话是否存在。 If not then call
openSession
method. 如果不是,则调用
openSession
方法。 It will open facebook login view. 它将打开Facebook登录视图。
There is a sample in the Facebook Github repository , showing how to switch between multiple users. Facebook Github存储库中有一个示例 ,显示了如何在多个用户之间切换。
It looks like it requires use of the custom login flow. 看起来它需要使用自定义登录流程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.