简体   繁体   English

facebook iOS sdk自定义委托和SSO授权

[英]facebook iOS sdk custom delegate and SSO authorization

I've been toying with the new facebook iOS sdk. 我一直在玩新的Facebook iOS SDK。 I have gotten my project to the point where someone can login successfully. 我的项目已经到有人可以成功登录的地步。 However I have 2 questions: 但是我有两个问题:

1) to hit the graph api you issue the following call: [facebookInstance requestWIthGraphPath:@"me" andDelegate:self]. 1)要打图api,请发出以下调用:[facebookInstance requestWIthGraphPath:@“ me” andDelegate:self]。 Is it possible to specificy a delegate other than self? 是否可以指定自己以外的代表? Currently all responses go to the (void)request: (FBRequest *) request didLOad:(id) result. 当前,所有响应都转到(void)request:(FBRequest *)请求didLOad:(id)结果。 But since my app may issue requests to the facebook api at different times and need different things to happen for each respective request issued, how can I specifiy which callback function the response should hit in my app? 但是,由于我的应用程序可能在不同时间向Facebook api发出请求,并且每个发出的请求都需要进行不同的处理,因此我如何指定响应应在我的应用程序中触发哪个回调函数? Is this possible? 这可能吗?

2) Once the user has logged in, how can you check their authorization/login status so that I can disable the login button if they are already logged in? 2)用户登录后,如何检查其授权/登录状态,以便如果他们已经登录,则可以禁用登录按钮? Consider the example of a user turning on the app for the 1st time and logging in. Then closing the app, and opening a few minutes later. 考虑一个用户第一次打开该应用程序并登录的示例。然后关闭该应用程序,然后在几分钟后打开。 I rather not show the user the login button the 2nd time and instead start pulling information to display such as their name. 我宁愿不第二次向用户显示登录按钮,而是开始拉取信息以显示诸如姓名。

1) There shouldn't be anything wrong with specifying a delegate other than self, as long as the object you provide as the delegate conforms to the FBRequestDelegate protocol. 1)只要您提供的对象符合FBRequestDelegate协议,指定self以外的委托就不会有什么问题。 Alternately you can interrogate the FBRequest object you get in the delegate method to determine which request it was that just loaded and what the appropriate response is. 或者,您可以查询在委托方法中获得的FBRequest对象,以确定刚刚加载的是哪个请求以及适当的响应是什么。

2) To make it so the user stays logged in, you need to save the accessToken (an NSString) and the expirationDate (an NSDate) properties of the Facebook object when the user logs in. Then, when you would log your user in, attempt to restore these values. 2)为了使其保持登录状态,您需要在用户登录时保存Facebook对象的accessToken(NSString)和expirationDate(NSDate)属性。然后,当您登录用户时,尝试还原这些值。 Some code snippets that may help: 一些代码片段可能会有所帮助:

- (void)fbDidLogin 
{
    NSString *tokenString = [[self facebook] accessToken];
    NSDate * expirationDate = [[self facebook] expirationDate];

    [[NSUserDefaults standardUserDefaults] setObject: tokenString forKey:@"FacebookToken"];
    [[NSUserDefaults standardUserDefaults] setObject: expirationDate forKey:@"FacebookExpirationDate"];
    [[NSUserDefaults standardUserDefaults] synchronize];    
}

And, when you need to log the user in: 并且,当您需要登录用户时:

NSString *tokenString = [[NSUserDefaults standardUserDefaults] objectForKey:@"FacebookToken"];    
NSDate *expDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"FacebookExpirationDate"];

if (tokenString != nil && expDate != nil)
{
    [facebook setAccessToken:tokenString];
    [facebook setExpirationDate:expDate];
}

if ([facebook isSessionValid])
{
    //Your session is valid, do whatever you want.
}
else 
{
    //Your session is invalid 
    //(either the session is past the expiration date or there is no saved data for the login)
    //you need to ask the user to log in
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM