UIWebView和NSURLConnection是否共享cookie存储? [英] Do UIWebView and NSURLConnection share cookie storage?

查看:110
本文介绍了UIWebView和NSURLConnection是否共享cookie存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建将Google App Engine用于后端的iOS应用。 Google提供了一个存储身份验证Cookie的HTML登录网站。如果我在UIWebView中访问该网站,并且用户登录,那么这些Cookie是否会存储在NSURLConnection向同一网站发出请求时将被拾取的存储中?

解决方案

UIWebView的cookie将存储在可通过 NSHTTPCookieStorage sharedHTTPCookieStorage] 访问的沙盒cookie存储中。您可以通过以下方式在NSURLConnection中使用此cookie存储:

  NSArray * cookies = [[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString :@ __ YOUR_URL__]]; 
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[request setAllHTTPHeaderFields:headers]; //先前创建的NSMutableURLRequest

现在你可以在NSURLConnection中正常使用NSURLRequest,在UIWebView登录后创建的cookie

I'm building an iOS app that uses Google App Engine for the backend. Google provides an HTML login site that stores an authentication cookie. If I visit that site in a UIWebView, and the user logs in, will those cookies be in storage where they will be picked up by a NSURLConnection when making a request to the same site?

解决方案

The cookie of the UIWebView will be stored in a sandboxed cookie storage accessible through NSHTTPCookieStorage sharedHTTPCookieStorage]. You can use this cookie storage in NSURLConnection in this way:

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"__YOUR_URL__"]];
NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[request setAllHTTPHeaderFields:headers]; //A previously created NSMutableURLRequest

Now you can normally use the NSURLRequest in a NSURLConnection and it will send the cookies created after the login in the UIWebView

这篇关于UIWebView和NSURLConnection是否共享cookie存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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