如何在iPhone应用程序中维护Web服务器的登录状态?

问题描述:

简单地说,我希望有一个iPhone应用程序使用输入的用户名和密码值登录到Web服务器,然后维护此登录,以便用户可以将数据POST到服务器。

Simply, I would like to have an iPhone app that logs in to a web server with inputted username and password values and then maintains this login, so that the user can POST data to the server.

这只是使用cookies的问题吗?如果是这样,你如何进行检查以验证用户是否已登录?

Is this just an issue of using cookies? If so, how do you perform a check to verify the user is logged in?

谢谢。

如果您无法控制Web服务器,登录过程就是我在我的某个应用程序中所做的:

If you don't have control over the web server and the login process here is what i've done in one of my apps:

[[NSString alloc] initWithContentsOfURL:someURL usedEncoding:&enc error:&error];

此方法已使用cookie,因此如果您已登录,您将保持登录状态。假设服务器将会话ID保存到cookie中,会话ID用于标识用户的会话(这是人们实现网站的正常方式)。

This method already uses cookies so if you are logged in you will stay logged in. Assuming that the server saves the session id into a cookie and the session id is used to identify a user's session (which is the normal way how someone would implement a website).

检查是否用户登录后您只需解析生成的网站即可。如果用户之前没有登录,大多数网站会将您的请求重定向到登录页面。

To check if a user is logged in you simply parse the resulting website. Most web sites will redirect your request to the login page if the user hasn't logged in before.

要澄清听到的步骤:


  1. 向需要登录的网址发送请求

  2. 检查结果

  3. 如果结果是登录页面 - >发送用户凭据并从1开始。

  4. 解析结果

  1. Send request to some url that needs login
  2. Check the result
  3. If the result was login page -> send user credentials and start from 1.
  4. Parse the result