如何在用户决定退出之前保持登录状态? [英] How to remain logged in until user decides to logout?

查看:167
本文介绍了如何在用户决定退出之前保持登录状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个使用Web服务的应用程序,比如发布链接和图片。第一个视图控制器检查用户的默认用户名和密码,如果它是正确的,它允许用户登录到主视图控制器,但如果它不正确,它会将用户引导到登录视图控制器(如果它是第一次应用程序正在运行它还会将用户定向到登录视图控制器。)

I am designing an app that it uses the web service like posting links and pictures. The first view controller checks the default username and password of the user and if it is correct, it allows the user to login to the home view controller but if its not correct it directs the user to the login view controller (if its the first time that the application is running it also directs the user to the login view controller).

我有不同的视图控制器连接到主视图控制器。例如,其中一个用于将图片发布到网站,另一个用于发布到Web的链接,另一个用于更改用户个人资料的首选项。

I have different view controllers that is connecting to the home view controller. For example one of them is for posting pictures to the website, the other one is for posting links to the web and the other view controller is for changing the preferences of the users profile.

我正在使用RestKit API在网上发布POST,这是我用于发布的代码:

I am using RestKit API to POST on web and here is the code that I use for posting:

- (IBAction)addLinkPressed:(UIButton *)sender {

        [RKClient clientWithBaseURLString:@"http://MyWebsite.com"];

        NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                                self.linkField.text, @"url",
                                self.linkTitleField.text, @"title",
                                self.linkSummaryField.text, @"summary",
                                nil];

        RKRequest *request = [[RKClient sharedClient] post:@"/send_link.php" params:params delegate:self];
        [request setUserData:@"sendLink"];   
}

对于每个视图控制器,我将以下方法放在中viewDidLoad 为了获得发布身份验证:

For each view controller I put the following method in viewDidLoad in order to get authentication for posting :

- (void)autoLogin {

    [RKClient clientWithBaseURLString:@"http://MyWebsite.com"];    
    [RKObjectManager sharedManager].client=[RKClient sharedClient];
    RKParams *parameters = [RKParams params];

    [parameters setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultUsername"] forParam:@"username"];
    [parameters setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultPassword"] forParam:@"password"];

    [[RKClient sharedClient] setAuthenticationType:RKRequestAuthenticationTypeHTTP];

    RKRequest *request = [[RKClient sharedClient] post:@"/login.php" params:parameters delegate:self];
    [request setUserData:@"login"];
}

问题:是否有任何方法可以从网站获取身份验证而不是登录每个视图控制器?登录每个视图控制器的问题是,某些时候日志记录过程会出错并且不允许用户进行POST。我需要一种方法,只要用户输入正确的用户名和密码,它就会一直登录,直到用户自己注销或删除应用程序。

Question: Is there any method to get authentication from the website for posting other than logging in for each view controller? the problem for logging in for each view controller is that some times logging process gives an error and does not allow the user to POST. I need a method that as soon as the user inputs the correct username and password, It remains logged in until the user logout by himself or delete the app.

我发现< a href =https://stackoverflow.com/questions/1519072/how-to-maintain-logged-in-status-with-web-server-in-an-iphone-app>这个相关问题但它没有那么有用,或者我没有很好地理解答案。任何想法或想法将不胜感激。

I found this relative question but It was not that helpful or I did not understand the answers well. Any thoughts or ideas would be appreciated.

推荐答案

RestKit旨在使用API​​。通常API是无状态的,这意味着请求之间不存在状态。在传统网站上,通过在客户端和服务器之间来回传输cookie来实现状态。显然RestKit不处理cookie(我认为这很好)。从头脑中我可以想到两个解决方案:

RestKit is designed to consume APIs. Usually APIs are stateless, meaning that there is no such thing as state between requests. On traditional websites state is achieved by transmitting cookies back and forth between client and server. Apparently RestKit does not handle cookies (which is good in my opinion). From the top of my head I can think of two solutions:


  • 显然RestKit不会自动处理cookie。因此,您可以在登录后获取cookie并将其附加到每个后续请求。

  • 更好:使用OAuth2,它基本上是用户在登录时收到的令牌。此令牌是然后作为参数附加到每个请求。

两种方法基本相同。您必须发送一个标记用户身份的令牌。

Both methods are basically the same. You have to send a token that identifies the user as logged in.

这篇关于如何在用户决定退出之前保持登录状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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