Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 目标c向uiwebview发送cookie_Ios_Objective C_Cookies_Uiwebview - Fatal编程技术网

Ios 目标c向uiwebview发送cookie

Ios 目标c向uiwebview发送cookie,ios,objective-c,cookies,uiwebview,Ios,Objective C,Cookies,Uiwebview,我需要发送Cookie以登录到UIWebView,但当我加载网页时,Cookie尚未发送 我的代码是: CorreoViewController.m - (void)viewDidLoad { [super viewDidLoad]; self.title =AMLocalizedString(@"Correo", @""); NSURL *url=[NSURL URLWithString:@"https://cuentas.uv.es/cgi-bin/p/user/U

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

我的代码是:

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.
}
- (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];
}
我的曲奇存储器中的曲奇是:

COOKIE 0:NSHTTPCookie版本:0名称:“PAPIAuthNcook” 值:“nBNqHJSdjgo1aBw0uGh3AXzElIHCxA%2FDbnipSHWZbU5Eh1cJfUr18GEP9zxB%2Bl%2fzgpbakqu6l%0Azimt7Y6Rvim2Hfe%3D%0A” expiresDate:(空)创建日期:2014-01-16 15:05:41+0000(4.11578e+08) sessionOnly:TRUE域:“cuentas.uv.es”路径:“/”isSecure:FALSE

COOKIE 1:NSHTTPCookie版本:0名称:“PAPIslist” 值:“17201128679244079 |”expiresDate:(空) 创建日期:2014-01-16 15:05:41+0000(4.11578e+08)仅会话:TRUE 域:“cuentas.uv.es”路径:“/”isSecure:FALSE

COOKIE 2:NSHTTPCookie版本:0名称:“PAPIAuthNcook” 值:“nBNqHJSdjgo1aBw0uGh3AXzElIHCxA%2FDbnipSHWZbU5Eh1cJfUr18GEP9zxB%2Bl%2fzgpbakqu6l%0Azimt7Y6Rvim2Hfe%3D%0A” expiresDate:(空)创建日期:2014-01-16 15:05:41+0000(4.11578e+08) sessionOnly:TRUE域:“as.uv.es”路径:“/”IsSecurity:FALSE

COOKIE 3:NSHTTPCookie版本:0名称:“PAPIslist” 值:“17201128679244079 |”expiresDate:(空) 创建日期:2014-01-16 15:05:41+0000(4.11578e+08)仅会话:TRUE 域:“as.uv.es”路径:“/”isSecure:FALSE

AppDelegate.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.
}
- (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];

cookies将自动发送。我希望这能为您服务。

我对以上几行进行了评论,但它不起作用。它是如何自动获取cookies的