UIWebView和NSURLConnection共享cookie存储吗?

时间:2021-07-24 20:17:37

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?

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

1 个解决方案

#1


22  

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:

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

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

现在您可以在NSURLConnection中正常使用NSURLRequest,它会在UIWebView中发送登录后创建的cookie

#1


22  

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:

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

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

现在您可以在NSURLConnection中正常使用NSURLRequest,它会在UIWebView中发送登录后创建的cookie