24

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

4

1 回答 1

22

UIWebView 的 cookie 将存储在沙盒 cookie 存储中,可通过NSHTTPCookieStorage sharedHTTPCookieStorage]. 你可以通过这种方式在 NSURLConnection 中使用这个 cookie 存储:

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

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

于 2013-03-13T23:08:22.837 回答