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

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

      <tfoot id='Sh9Wp'></tfoot>
    2. UIWebview 启用cookies

      UIWebview enable cookies(UIWebview 启用cookies)
      <tfoot id='CsQOZ'></tfoot>
    3. <small id='CsQOZ'></small><noframes id='CsQOZ'>

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

                <tbody id='CsQOZ'></tbody>

                <bdo id='CsQOZ'></bdo><ul id='CsQOZ'></ul>
                本文介绍了UIWebview 启用cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试通过 UIWebView 访问需要 cookie 的特定 URL,但我无法访问它,因为 cookie 被禁用.所以我做了以下事情:

                I am trying to access a specific URL that requires cookies through UIWebView but I can not access it because cookies are disabled. So I did the following:

                • 启用 cookie:

                • Enabled cookies:

                NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
                
                [cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
                

              • 创建 NSURLConnection 并从响应中提取 cookie:

              • Created NSURLConnection and extracted cookies from response:

                NSArray *cookies = [ NSHTTPCookie cookiesWithResponseHeaderFields: [ httpResponse allHeaderFields ] forURL:response.URL];
                
                [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies: cookies forURL: response.URL mainDocumentURL:nil];
                

              • 但这都没有帮助.但是,如果我在 safari 中启动 URL,它会成功加载,之后我也可以在 UIWebView 中加载相同的 URL.你能帮我解决这个问题,我怎样才能为 UIWebView 启用 cookie?

                But neither of this didn't help. However if I launch the URL in safari it loads successfully and after that I can load the same URL in UIWebView too. Could you help me with this, how can I enable cookies for UIWebView?

                提前致谢

                推荐答案

                创建NSURLRequest后,将sharedHTTPCookieStorage中的所有cookie复制到NSURLRequest:

                After create a NSURLRequest, copy all cookies in sharedHTTPCookieStorage to NSURLRequest:

                NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
                
                NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
                [request setHTTPShouldHandleCookies:YES];
                [self addCookies:cookies forRequest:request];
                [_webView loadRequest:request];
                

                并添加addCookies:forRequest方法

                - (void)addCookies:(NSArray *)cookies forRequest:(NSMutableURLRequest *)request
                {
                    if ([cookies count] > 0)
                    {
                        NSHTTPCookie *cookie;
                        NSString *cookieHeader = nil;
                        for (cookie in cookies)
                        {
                            if (!cookieHeader)
                            {
                                cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[cookie value]];
                            }
                            else
                            {
                                cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie value]];
                            }
                        }
                        if (cookieHeader)
                        {
                            [request setValue:cookieHeader forHTTPHeaderField:@"Cookie"];
                        }
                    }
                }
                

                这篇关于UIWebview 启用cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                iOS UIWebView app opens link in Safari(iOS UIWebView 应用在 Safari 中打开链接)
                Close UIWebView using javascript:window.close();(使用 javascript:window.close() 关闭 UIWebView;)
                NSCachedURLResponse returns object, but UIWebView does not interprets content(NSCachedURLResponse 返回对象,但 UIWebView 不解释内容)
                Trouble hiding iAd banner and displaying UIWebView in its place(无法隐藏 iAd 横幅并在其位置显示 UIWebView)
                Knowing when AJAX has loaded in UIWebView(知道何时在 UIWebView 中加载了 AJAX)
                Load image/logo in existing HTML input field - UIWebView(在现有的 HTML 输入字段中加载图像/徽标 - UIWebView)
                <legend id='KG7D7'><style id='KG7D7'><dir id='KG7D7'><q id='KG7D7'></q></dir></style></legend>

                    <small id='KG7D7'></small><noframes id='KG7D7'>

                    • <bdo id='KG7D7'></bdo><ul id='KG7D7'></ul>

                      <tfoot id='KG7D7'></tfoot>

                        <tbody id='KG7D7'></tbody>

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