<legend id='LD86A'><style id='LD86A'><dir id='LD86A'><q id='LD86A'></q></dir></style></legend>
        <bdo id='LD86A'></bdo><ul id='LD86A'></ul>
      <tfoot id='LD86A'></tfoot>

      1. <small id='LD86A'></small><noframes id='LD86A'>

        <i id='LD86A'><tr id='LD86A'><dt id='LD86A'><q id='LD86A'><span id='LD86A'><b id='LD86A'><form id='LD86A'><ins id='LD86A'></ins><ul id='LD86A'></ul><sub id='LD86A'></sub></form><legend id='LD86A'></legend><bdo id='LD86A'><pre id='LD86A'><center id='LD86A'></center></pre></bdo></b><th id='LD86A'></th></span></q></dt></tr></i><div id='LD86A'><tfoot id='LD86A'></tfoot><dl id='LD86A'><fieldset id='LD86A'></fieldset></dl></div>
      2. 两个 UIWebView 有单独的 cookie 存储吗?

        时间:2023-10-22

            <tbody id='sPzt0'></tbody>

        • <small id='sPzt0'></small><noframes id='sPzt0'>

          • <legend id='sPzt0'><style id='sPzt0'><dir id='sPzt0'><q id='sPzt0'></q></dir></style></legend>
              <bdo id='sPzt0'></bdo><ul id='sPzt0'></ul>

                <i id='sPzt0'><tr id='sPzt0'><dt id='sPzt0'><q id='sPzt0'><span id='sPzt0'><b id='sPzt0'><form id='sPzt0'><ins id='sPzt0'></ins><ul id='sPzt0'></ul><sub id='sPzt0'></sub></form><legend id='sPzt0'></legend><bdo id='sPzt0'><pre id='sPzt0'><center id='sPzt0'></center></pre></bdo></b><th id='sPzt0'></th></span></q></dt></tr></i><div id='sPzt0'><tfoot id='sPzt0'></tfoot><dl id='sPzt0'><fieldset id='sPzt0'></fieldset></dl></div>
              1. <tfoot id='sPzt0'></tfoot>
                  本文介绍了两个 UIWebView 有单独的 cookie 存储吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  背景
                  我正在开发一个简单的 iPad 应用程序,它允许用户同时使用不同的登录名浏览同一个网站.因此,我有两个 UIWebView 并且它们应该有不同的 cookie 存储,因此用户可以在第一个 UIWebView 上登录一个帐户,在第二个 UIWebView 上登录另一个帐户>.

                  Background
                  I am developing a simple iPad application that allow the user to browse the same website with different logins at the same time. Therefore I have two UIWebView and they should have different cookie storage so the user can login one account on the first UIWebView and another account on the second UIWebView.

                  我尝试了什么?
                  我认为解决方案是在我拥有的两个 UIWebView 中实现不同的 cookie 存储.

                  What have I tried?
                  I think the solution is to implement different cookie storages in the two UIWebView I have.

                  Sasmito Adibowo 写了一篇文章实施您自己的 Cookie 存储,其中详细介绍了如何在 Mac 上为 WebView 使用自定义 cookie 存储.
                  它是通过修改 WebView 将要发送的 NSURLRequest 来完成的,向其中添加 cookie 标头,并拦截来自 WebView 的响应并从响应标头中提取 cookie 并将其保存到我们自己的 cookie 存储中.
                  从技术上讲,它是通过实现这两个委托方法来完成的:

                  Sasmito Adibowo wrote an article Implementing Your Own Cookie Storage which provide details on how to use a custom cookie storage for WebView on Mac.
                  It is done by modify the NSURLRequest that WebView is going to send, adding cookie headers to it, and also intercept the response from WebView and extract the cookies from the response header and save it to our own cookie storage.
                  Technically, it is done by implementing these two delegate methods:

                  - (void)webView:(WebView *)sender resource:(id)identifier didReceiveResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)dataSource
                  - (NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource
                  

                  虽然没有记录,但 UIWebView 确实支持上述方法之一,但方法名称略有不同:

                  Although it is undocumented, UIWebView did support one of the method above with a slightly different method name:

                  - (NSURLRequest *)uiWebView:(UIWebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(id)dataSource
                  

                  但是,UIWebView 没有用于 webView:resource:didReceiveResponse:fromDataSource: 的等效委托方法,因此我无法从响应标头中提取 cookie.

                  However, UIWebView don't have a equivalent delegate method for webView:resource:didReceiveResponse:fromDataSource: and hence I cannot extract the cookies from the response headers.

                  问题
                  有没有办法让UIWebView使用自定义的cookie存储,让两个UIWebView可以有自己的cookie存储?

                  The Question
                  Is there a way to have UIWebView to use a custom cookie storage, so the two UIWebView can have their own cookie storage?

                  谢谢!

                  推荐答案

                  您是否尝试过在 webViewDidStartLoad 中获取与特定 webview 关联的 cookie(并保留它们):

                  Have you tried getting the cookies associated with a particular webview (and holding onto them) in webViewDidStartLoad:

                  NSHTTPCookie *cookie;
                  NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
                  for (cookie in [cookieJar cookies]) {
                      [self.cookies addObject:cookie];
                  }
                  

                  并在之后存储这些 cookie(从 self.cookies 中检索值和键):

                  And storing these cookies right after (retrieve values and keys from self.cookies):

                  NSMutableDictionary *cookieDict = [NSMutableDictionary dictionary];
                  [cookieDict setObject:@"value1" forKey:NSHTTPCookieName];
                  [cookieDict setObject:@"value2" forKey:NSHTTPCookieValue];
                  [cookieDict setObject:@"value3" forKey:NSHTTPCookieDomain];
                  ...etc..
                  
                  NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieDict];
                  [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
                  

                  您还需要在 viewDidLoad 中看到这一点:

                  You'll also need to see this in your viewDidLoad:

                  [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
                  

                  这篇关于两个 UIWebView 有单独的 cookie 存储吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:UIWebView 占用大量内存 下一篇:uiwebview 和巨大的内存丢失

                  相关文章

                  <i id='h0Kw0'><tr id='h0Kw0'><dt id='h0Kw0'><q id='h0Kw0'><span id='h0Kw0'><b id='h0Kw0'><form id='h0Kw0'><ins id='h0Kw0'></ins><ul id='h0Kw0'></ul><sub id='h0Kw0'></sub></form><legend id='h0Kw0'></legend><bdo id='h0Kw0'><pre id='h0Kw0'><center id='h0Kw0'></center></pre></bdo></b><th id='h0Kw0'></th></span></q></dt></tr></i><div id='h0Kw0'><tfoot id='h0Kw0'></tfoot><dl id='h0Kw0'><fieldset id='h0Kw0'></fieldset></dl></div>

                      <bdo id='h0Kw0'></bdo><ul id='h0Kw0'></ul>
                  1. <small id='h0Kw0'></small><noframes id='h0Kw0'>

                  2. <tfoot id='h0Kw0'></tfoot>

                    <legend id='h0Kw0'><style id='h0Kw0'><dir id='h0Kw0'><q id='h0Kw0'></q></dir></style></legend>