Facebook iOS SDK 是否要求用户每次使用该应用程序时都进行身份验证?

发布于 2024-10-30 08:16:44 字数 453 浏览 0 评论 0原文

如 facebook-ios-sdk 的自述文件中所述,我的应用程序在执行任何 API 调用之前调用 Facebook#authorize:delegate:。

此方法要求用户进行身份验证(在 Facebook 应用程序或 Safari 中),然后将控制权交还给我的 iPhone 应用程序。问题是每次我调用该方法时它都会要求用户进行身份验证。如果他们已经向我的应用程序授予了权限,他们会收到一条消息,指出该应用程序已获得授权,并且他们必须按“确定”才能返回到我的应用程序。看起来不是很专业。

所以我有两个问题:

  1. 用户是否总是需要重新授权才能拨打 Facebook 电话?我一直认为它会将访问令牌保存在某个地方,也许是在用户默认值中,这样您就不需要重新授权。

  2. 如果用户不必每次都重新授权,是否有办法检查我的应用程序是否已拥有权限,这样用户就不必看到该消息并按“确定”?

As described in the README for facebook-ios-sdk, my app calls Facebook#authorize:delegate: before performing any API calls.

This method requires the user to authenticate (either in the Facebook app or in Safari) and then drops control back to my iPhone app. The problem is it asks the user to authenticate every time I call the method. If they've already granted permission to my app, they get a message saying that the app is already authorized and they have to press Okay to go back to my app. It doesn't look very professional.

So I have two questions:

  1. Does the user always have to reauthorize in order to make Facebook calls? I always thought it would save the access token somewhere, maybe in the user defaults, so that you wouldn't need to reauthorize.

  2. If the user doesn't have to reauthorize every time, is there a way to check if my app already has permission, so the user doesn't have to see that message and press Okay?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南薇 2024-11-06 08:16:45

不幸的是 Facebook SDK 不会自动为我们处理它。除了自己将其构建到 SDK 中之外,最简单的方法是这样的:

每次分配 _facebook:

_facebook = [[Facebook alloc] initWithAppId:@"SuperDuperAppID"];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *accessToken = [prefs stringForKey:@"facebook-accessToken"];
NSString *expirationDate = [prefs stringForKey:@"facebook-expirationDate"];
_facebook.accessToken = accessToken;
_facebook.expirationDate = expirationDate;

如果您从未在 NSUserDefaults 中保存过任何内容,则将设置 nil 值。什么都没发生。否则,将设置 true 值,并且 [_facebook isSessionValid]; 应返回 TRUE,表示良好的值。继续进行 Facebook SDK 调用,就好像他们已经登录一样(确实如此)。如果为 false,则

[facebook authorize:permissions delegate:self]; 

照常调用。

然后确保支持以下 Facebook 委托方法:

- (void)fbDidLogin {
NSString *accessToken = _facebook.accessToken;
NSString *expirationDate = _facebook.expirationDate;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:accessToken forKey:@"facebook-accessToken"];
[prefs expirationDate forKey:@"facebook-expirationDate"];
[prefs synchronize];
}

我在我的应用程序中使用此代码,并且它运行良好。我是凭记忆做的,所以如果复制粘贴不起作用,我深表歉意。某处可能存在打字错误,加上内存管理不当。不要忘记,除非您获得离线许可,否则访问令牌会过期。无论如何,上面的代码可以处理所有事情。

It's unfortunate that the Facebook SDK doesn't automatically handle it for us. Aside from building it into the SDK yourself, the easiest way is as such:

Each time you allocate _facebook:

_facebook = [[Facebook alloc] initWithAppId:@"SuperDuperAppID"];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *accessToken = [prefs stringForKey:@"facebook-accessToken"];
NSString *expirationDate = [prefs stringForKey:@"facebook-expirationDate"];
_facebook.accessToken = accessToken;
_facebook.expirationDate = expirationDate;

If you've never saved anything in the NSUserDefaults, then nil values will be set. Nothing's happened. Otherwise, true values will be set, and [_facebook isSessionValid]; should return TRUE, indicating good values. Go ahead and make Facebook SDK calls as if they are logged in (which they are). If false, then call

[facebook authorize:permissions delegate:self]; 

as usual.

Then make sure to support the following Facebook delegate method:

- (void)fbDidLogin {
NSString *accessToken = _facebook.accessToken;
NSString *expirationDate = _facebook.expirationDate;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:accessToken forKey:@"facebook-accessToken"];
[prefs expirationDate forKey:@"facebook-expirationDate"];
[prefs synchronize];
}

I use this code in my app, and it works perfectly. I did this from memory, so I apologize if it doesn't work on copy-and-paste. There might be a mistype somewhere, plus improper memory management. Don't forget that access tokens expire unless you get offline permission. In any case the code above handles everything.

浪推晚风 2024-11-06 08:16:45

问题是它要求用户
每次我打电话时都进行身份验证
方法。如果他们已经授予
获得我的应用程序的许可,他们会得到
消息说该应用程序已经
授权,他们必须按“确定”
返回我的应用程序。

1) 用户是否总是必须
重新授权才能使 Facebook
打电话?我一直以为这样可以拯救
某处的访问令牌,可能在
用户默认,这样你
不需要重新授权。

每次都必须执行登录过程的原因是,在第一次成功登录后,Facebook SDK 不会以持久方式保存访问令牌

2) 如果用户不需要
每次都重新授权,有办法吗
检查我的应用程序是否已经有
权限,所以用户没有
查看该消息并按确定?

Facebook SDK 不保存访问令牌这一事实并不意味着您不能自己执行此操作。以下代码说明了如何从 NSUserDefaults 保存/检索访问令牌。

-(void)login {
    // on login, use the stored access token and see if it still works
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    facebook.accessToken = [defaults objectForKey:ACCESS_TOKEN_KEY];
    facebook.expirationDate = [defaults objectForKey:EXPIRATION_DATE_KEY];

    // only authorize if the access token isn't valid
    // if it *is* valid, no need to authenticate. just move on
    if (![facebook isSessionValid]) {
        NSArray * permissions = [NSArray arrayWithObjects:@"read_stream", @"offline_access", @"email", nil];
        [facebook authorize:permissions delegate:self];
    }
}

/**
 * Called when the user has logged in successfully.
 */
- (void)fbDidLogin {
    // store the access token and expiration date to the user defaults
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:facebook.accessToken forKey:ACCESS_TOKEN_KEY];
    [defaults setObject:facebook.expirationDate forKey:EXPIRATION_DATE_KEY];
    [defaults synchronize];
}

The problem is it asks the user to
authenticate every time I call the
method. If they've already granted
permission to my app, they get a
message saying that the app is already
authorized and they have to press Okay
to go back to my app.

1) Does the user always have to
reauthorize in order to make Facebook
calls? I always thought it would save
the access token somewhere, maybe in
the user defaults, so that you
wouldn't need to reauthorize.

The cause of having to go through the sign on process every time is that, after successfully signing on the first time, the Facebook SDK doesn't save the access token in a persistent manner.

2) If the user doesn't have to
reauthorize every time, is there a way
to check if my app already has
permission, so the user doesn't have
to see that message and press Okay?

The fact that the Facebook SDK doesn't save the access token doesn't mean you can't do it yourself. The following code ilustrates how to save/retrieve the access token from the NSUserDefaults.

-(void)login {
    // on login, use the stored access token and see if it still works
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    facebook.accessToken = [defaults objectForKey:ACCESS_TOKEN_KEY];
    facebook.expirationDate = [defaults objectForKey:EXPIRATION_DATE_KEY];

    // only authorize if the access token isn't valid
    // if it *is* valid, no need to authenticate. just move on
    if (![facebook isSessionValid]) {
        NSArray * permissions = [NSArray arrayWithObjects:@"read_stream", @"offline_access", @"email", nil];
        [facebook authorize:permissions delegate:self];
    }
}

/**
 * Called when the user has logged in successfully.
 */
- (void)fbDidLogin {
    // store the access token and expiration date to the user defaults
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:facebook.accessToken forKey:ACCESS_TOKEN_KEY];
    [defaults setObject:facebook.expirationDate forKey:EXPIRATION_DATE_KEY];
    [defaults synchronize];
}
绝情姑娘 2024-11-06 08:16:45

当用户登录时,您将获得访问令牌和到期日期。保存访问令牌,只要尚未到期,就可以重复使用它。 Facebook SDK 有一个方法可以执行此检查:[fbConnect isSessionValid] 但有时它似乎会给出误报,因此如果请求失败,我会让用户登录。

When the user logs in, you'll get an access token and an expiry date. Save the access token and you can reuse it as long as you haven't hit the expiry date yet. The Facebook SDK has a method to do this check: [fbConnect isSessionValid] but it seems to give false positives sometimes, so I have the user log in if a request fails.

征棹 2024-11-06 08:16:45

1)不,我必须这样做 Facebook *fbConnect = [[Facebook alloc] init]; [fbConnect logout:self]; 以获取用户会话

2) 我想您可以在 Facebook.m 中调用 accestoken 来检查

1) No, I had to do Facebook *fbConnect = [[Facebook alloc] init]; [fbConnect logout:self]; to get the user session away

2) I suppose you could call the accestoken in Facebook.m to check that

葬心 2024-11-06 08:16:45

这是 @Daniel Amitay 回答的更实际的方法:
在带有登录按钮的视图控制器中,您

//saving user data to user defaults in order to support Facebook SSO
let token = FBSDKAccessToken.currentAccessToken()
let prefs = NSUserDefaults.standardUserDefaults()
let nsdataToken = NSKeyedArchiver.archivedDataWithRootObject(token)
prefs.setObject(nsdataToken, forKey: "facebook-AccessToken")
prefs.synchronize()

在 AppDelegate 文件 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> 中 写入布尔型
你写以下

//restoring Facebook SSO session from user defaults
let prefs = NSUserDefaults.standardUserDefaults()
if let nsdataToken = prefs.valueForKey("facebook-AccessToken") as? NSData {
    let token = NSKeyedUnarchiver.unarchiveObjectWithData(nsdataToken) as! FBSDKAccessToken
    FBSDKAccessToken.setCurrentAccessToken(token)
}

Here is more actual approach of @Daniel Amitay answer:
In view controller with login button you write

//saving user data to user defaults in order to support Facebook SSO
let token = FBSDKAccessToken.currentAccessToken()
let prefs = NSUserDefaults.standardUserDefaults()
let nsdataToken = NSKeyedArchiver.archivedDataWithRootObject(token)
prefs.setObject(nsdataToken, forKey: "facebook-AccessToken")
prefs.synchronize()

In AppDelegate file func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
you write following

//restoring Facebook SSO session from user defaults
let prefs = NSUserDefaults.standardUserDefaults()
if let nsdataToken = prefs.valueForKey("facebook-AccessToken") as? NSData {
    let token = NSKeyedUnarchiver.unarchiveObjectWithData(nsdataToken) as! FBSDKAccessToken
    FBSDKAccessToken.setCurrentAccessToken(token)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文