本文介绍了目标c发送cookie到uiwebview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发送Cookie以登录到UIWebView,但是当我加载网页时,Cookie尚未发送。

I need to send cookies for login to a UIWebView, but when I load the web page the cookies haven't been sent.

我的代码是:

CorreoViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title =AMLocalizedString(@"Correo", @"");
    NSURL *url=[NSURL URLWithString:@"https://cuentas.uv.es/cgi-bin/p/user/Usuario"];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];

    NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];

    [request setHTTPMethod:@"Post"];
    [request setHTTPShouldHandleCookies:YES];
    [request setAllHTTPHeaderFields:headers];

    NSLog(@"Request: %@", [[request allHTTPHeaderFields] objectForKey:@"cookie"]);

    [self.webView loadRequest:request];
    // Do any additional setup after loading the view.
}

我在我的cookie存储中的cookie是:

And the cookies I have in my cookie storage are:

COOKIE 0:NSHTTPCookie版本:0名:PAPIAuthNcook
值:nBNqHJSdjgo1aBw0uGh3AXzElIHCxA%2FDbnipSHWZbU5Eh1cJfUr18GEP9zxB%2BL%2FZGPBAKQu6oF2l%0AzIMt7ywiSECLBuOHJnvS7y6rVim2HfE%3D%0A
expiresDate:(null)created:2014-01-16 15:05:41 +0000(4.11578e + 08)
sessionOnly:TRUE domain:cuentas.uv.espath:/的isSecure:FALSE

COOKIE 1:NSHTTPCookie版本:0名:PAPIslist
值:17201128679244079 |的expiresDate:(null)
创建时间:2014-01-16 15:05:41 +0000 4.11578e + 08)sessionOnly:TRUE
区:cuentas.uv.es路径:/的isSecure:FALSE

COOKIE 1: NSHTTPCookie version:0 name:"PAPIslist" value:"17201128679244079|https://uvapp.uv.es/" expiresDate:(null) created:2014-01-16 15:05:41 +0000 (4.11578e+08) sessionOnly:TRUE domain:"cuentas.uv.es" path:"/" isSecure:FALSE

2 COOKIE:NSHTTPCookie版本: 0名:PAPIAuthNcook
值:nBNqHJSdjgo1aBw0uGh3AXzElIHCxA%2FDbnipSHWZbU5Eh1cJfUr18GEP9zxB%2BL%2FZGPBAKQu6oF2l%0AzIMt7ywiSECLBuOHJnvS7y6rVim2HfE%3D%0A
expiresDate:(空)发布:2014年1月16日15时05分41秒+0000 (4.11578e + 08)
sessionOnly:TRUE域:as.uv.es路径:/的isSecure:FALSE

COOKIE 2: NSHTTPCookie version:0 name:"PAPIAuthNcook" value:"nBNqHJSdjgo1aBw0uGh3AXzElIHCxA%2FDbnipSHWZbU5Eh1cJfUr18GEP9zxB%2Bl%2FZGPBAKQu6oF2l%0AzIMt7ywiSECLBuOHJnvS7y6rVim2HfE%3D%0A" expiresDate:(null) created:2014-01-16 15:05:41 +0000 (4.11578e+08) sessionOnly:TRUE domain:"as.uv.es" path:"/" isSecure:FALSE

3 COOKIE:NSHTTPCookie版本:0 name:PAPIslist
value:17201128679244079 | expiresDate:(null)
created:2014-01-16 15:05:41 +0000(4.11578e + 08)sessionOnly:TRUE
domain:as.uv.espath: /isSecure:FALSE

COOKIE 3: NSHTTPCookie version:0 name:"PAPIslist" value:"17201128679244079|https://uvapp.uv.es/" expiresDate:(null) created:2014-01-16 15:05:41 +0000 (4.11578e+08) sessionOnly:TRUE domain:"as.uv.es" path:"/" isSecure:FALSE

AppDelegate.m

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [[NSHTTPCookieStorage sharedHTTPCookieStorage]setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
}

提前感谢。

推荐答案

评论此行:

 NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];
[request setAllHTTPHeaderFields:headers];

Cookie会自动发送。我希望这将为你服务。

the cookies are sent automatically. I hope this will serve you.

这篇关于目标c发送cookie到uiwebview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 11:23