无法在iOS中的url上设置cookie

时间:2014-06-04 06:50:04

标签: ios objective-c cookies

我有一个应用程序,其中一个我的网址我正在获取cookie,我循环遍历cookie数组以获取我需要的cookie,然后在我的webviewcontroller上我设置了这个cookie。问题是我能够获取cookie但无法设置cookie。我已经正确完成了代码,但不知道为什么没有在iOS中设置cookie。

这是我获取cookie的代码:

NSURL* aUrl =
      [NSURL URLWithString:@"https://www.cookie.com/session/create"];
  NSMutableURLRequest* request =
      [NSMutableURLRequest requestWithURL:aUrl
                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                          timeoutInterval:30.0];
  [request setHTTPMethod:@"GET"];
  NSError* error = nil;
  NSHTTPURLResponse* res = nil;
  NSData* returnData = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:&res
                                                         error:&error];
  NSArray* httpscookies =
      [NSHTTPCookie cookiesWithResponseHeaderFields:[res allHeaderFields]
                                             forURL:aUrl];
  for (int i = 0; i < [httpscookies count]; i++) {
    cookie = [httpscookies objectAtIndex:i];
  }

  [[NSHTTPCookieStorage sharedHTTPCookieStorage]
           setCookies:httpscookies
               forURL:[NSURL URLWithString:@"https://www.session.com/"]
      mainDocumentURL:nil];

我可以在httpscookies var

中获取cookie数组
(
<NSHTTPCookie version:0 name:"JSESSIONID" value:"03C4DB8AE44C53A9E552E1312D6F204D.pgnode1" expiresDate:(null) created:2014-06-04 06:35:11 +0000 (4.23557e+08) sessionOnly:TRUE domain:"www.session.com" path:"/" isSecure:TRUE>,
<NSHTTPCookie version:0 name:”mycookie” value:”aeftfec51c4d64bb6a258d53d6ebe8f10b801e047e2d387cef3b770399c16bc1e6cc9ce5167ceab7a6d9d3af1d8351a7b07cf04129f149a" expiresDate:2015-06-04 06:38:15 +0000 created:2014-06-04 06:35:11 +0000 (4.23557e+08) sessionOnly:FALSE domain:”www.session.com” path:"/" isSecure:TRUE>
)

这是将Cookie设置为我的网络视图的代码

NSArray* cookies;
  NSDictionary* cookieHeaders;
  NSMutableURLRequest* request;

  cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
  if (!cookies) {
    return;
  }

  [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies
                                                     forURL:webviewurl
                                            mainDocumentURL:nil];

// webviewurl是网络视图的网址。我需要'mycookie'值才能在webview上设置。

1 个答案:

答案 0 :(得分:0)

试试这个对我有用。使用下面的代码你可以存储cookie。

NSString *urlString =
    [NSString stringWithFormat:@"http://www.some url%@",
     [newuaernamedsde stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURL *URL = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    responseData = [NSMutableData data];
    NSURLConnection * connection = [[NSURLConnection alloc]
                                    initWithRequest:request
                                    delegate:self startImmediately:NO];

    [connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                          forMode:NSDefaultRunLoopMode];
    [connection start];


    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [cookieJar cookies]) 
    {
     //   NSLog(@"%@cookiecookiecookiecookie", cookie);
    }

此处使用以下代码,您可以将cookie设置为我的网页视图

- (void)viewDidLoad
{
        NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:geturllink]];
        [request setHTTPShouldHandleCookies:YES];
        [self addCookies:cookies forRequest:request];
        [webpageeee loadRequest:request];
}

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