Facebook iOS SDK 是否要求用户每次使用该应用程序时都进行身份验证?
如 facebook-ios-sdk 的自述文件中所述,我的应用程序在执行任何 API 调用之前调用 Facebook#authorize:delegate:。
此方法要求用户进行身份验证(在 Facebook 应用程序或 Safari 中),然后将控制权交还给我的 iPhone 应用程序。问题是每次我调用该方法时它都会要求用户进行身份验证。如果他们已经向我的应用程序授予了权限,他们会收到一条消息,指出该应用程序已获得授权,并且他们必须按“确定”才能返回到我的应用程序。看起来不是很专业。
所以我有两个问题:
用户是否总是需要重新授权才能拨打 Facebook 电话?我一直认为它会将访问令牌保存在某个地方,也许是在用户默认值中,这样您就不需要重新授权。
如果用户不必每次都重新授权,是否有办法检查我的应用程序是否已拥有权限,这样用户就不必看到该消息并按“确定”?
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:
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不幸的是 Facebook SDK 不会自动为我们处理它。除了自己将其构建到 SDK 中之外,最简单的方法是这样的:
每次分配 _facebook:
如果您从未在 NSUserDefaults 中保存过任何内容,则将设置 nil 值。什么都没发生。否则,将设置 true 值,并且
[_facebook isSessionValid];
应返回 TRUE,表示良好的值。继续进行 Facebook SDK 调用,就好像他们已经登录一样(确实如此)。如果为 false,则照常调用。
然后确保支持以下 Facebook 委托方法:
我在我的应用程序中使用此代码,并且它运行良好。我是凭记忆做的,所以如果复制粘贴不起作用,我深表歉意。某处可能存在打字错误,加上内存管理不当。不要忘记,除非您获得离线许可,否则访问令牌会过期。无论如何,上面的代码可以处理所有事情。
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:
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 callas usual.
Then make sure to support the following Facebook delegate method:
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.
每次都必须执行登录过程的原因是,在第一次成功登录后,Facebook SDK 不会以持久方式保存访问令牌。
Facebook SDK 不保存访问令牌这一事实并不意味着您不能自己执行此操作。以下代码说明了如何从 NSUserDefaults 保存/检索访问令牌。
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.
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.
当用户登录时,您将获得访问令牌和到期日期。保存访问令牌,只要尚未到期,就可以重复使用它。 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.
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 away2) I suppose you could call the accestoken in
Facebook.m
to check that这是 @Daniel Amitay 回答的更实际的方法:
在带有登录按钮的视图控制器中,您
在 AppDelegate 文件
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> 中 写入布尔型
你写以下
Here is more actual approach of @Daniel Amitay answer:
In view controller with login button you write
In AppDelegate file
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
you write following