AFNetworking和Cookies [英] AFNetworking and Cookies
问题描述
我将AFNetworking用作iPhone应用程序的网络层,该网络层连接到使用Devise进行身份验证的Rails服务器。如果我登录(提供POST呼叫)并提供用户名/密码,那么之后执行的任何GET操作都可以。
I'm using AFNetworking as a network layer for my iPhone app which connects to a Rails server that uses Devise for authentication. If I sign in (with a POST call) providing username/password then after that any GET I perform is ok.
如果我关闭应用程序(而不仅仅是后台),则
If I close the app (not just background) then all my GET requests fail because I guess they're not authenticated.
所以我认为cookie存储在某个地方;有没有一种方法可以将它们保存在 NSUserDefaults
或类似的目录中,以便避免始终登录?
So I presume cookies are stored somewhere; is there a way to save them in NSUserDefaults
or somewhere like that in order to avoid logging in all the time?
推荐答案
对于特定服务器上的任何后续请求,Cookie确实会在应用程序的生命周期内自动存储。一个好的策略是将用户名和密码存储在钥匙串或 NSUserDefaults
中,如下所示:
Cookies are indeed automatically stored for the lifetime of your application for any subsequent requests on a particular server. A good strategy would be to store the username and password in the keychain or in NSUserDefaults
like this:
// Setting
[[NSUserDefaults standardDefaults] setObject:username forKey:@"username"];
[[NSUserDefaults standardDefaults] synchronize];
// Getting
NSString *username = [[NSUserDefaults standardDefaults] objectForKey:@"username"];
您可能希望将其与 AFHTTPClient $ c $结合使用c>在
Authorization
HTTP标头中发送凭据和每个请求。
You may want to use this in combination with AFHTTPClient
to send your credentials along with every request in an Authorization
HTTP header.
这篇关于AFNetworking和Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!