Facebook iOS SDK是否要求用户在每次使用该应用程序时进行身份验证? [英] Does the Facebook iOS SDK require the user to authenticate every time they use the app?

查看:152
本文介绍了Facebook iOS SDK是否要求用户在每次使用该应用程序时进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这个方法需要用户验证(在Facebook应用程序或Safari中),然后将控制权重新放回到我的iPhone应用程序。问题是它要求用户在每次调用该方法时进行身份验证。如果他们已经授权我的应用程序,他们会收到一条消息,指出该应用已经被授权,他们必须按OK才能返回到我的应用。这不是很专业。



所以我有两个问题:


  1. 用户是否必须重新授权才能进行Facebook通话?我一直以为会将访问令牌保存在某个地方,也许在用户默认情况下,不需要重新授权。


  2. 如果用户没有不得不每次重新授权,是否有办法检查我的应用程序是否已经拥有权限,因此用户不必看到该消息并按OK?



解决方案

不幸的是,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值。没有发生否则,将设置真值, [_ 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];
}

我在我的应用程序中使用这个代码,它的工作原理很好。我从记忆中做到这一点,所以如果在复制和粘贴上不起作用,我很抱歉。在某个地方可能会有一个错误的类型,加上不正确的内存管理。不要忘记访问令牌到期,除非您获得离线许可。无论如何,上面的代码处理一切。


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?

解决方案

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.

这篇关于Facebook iOS SDK是否要求用户在每次使用该应用程序时进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆