0

我正在尝试从 NSHTTPCookieStorage 获取 cookie 并将其用于我的应用程序中的另一个 URL。我正在从 NSHTTPCookieStorage 复制一个 cookie,然后使用复制的 cookie 在 NSHTTPCookieStorage 中为不同的 URL 设置 cookie。我正在尝试遵循代码,但是当我打印出另一个 URL 的 cookie 时,没有。

    // authCookie is a cookie already in NSHTTPCookieStorage.
    NSURL *searchURL = [NSURL URLWithString:searchURLString];
    NSDictionary *authCookieProperties = [[[authCookie properties] copy] autorelease];
    NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionaryWithDictionary:authCookieProperties];

    [cookieProperties setObject:searchURL forKey:NSHTTPCookieDomain];
    [cookieProperties setObject:searchURL forKey:NSHTTPCookieOriginURL];
    NSHTTPCookie *searchCookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
    [sharedHTTPCookieStorage setCookie:searchCookie];
    [sharedHTTPCookieStorage setCookies:[NSArray arrayWithObject:searchCookie] 
                                 forURL:searchURL 
                        mainDocumentURL:nil];

    NSLog(@"Cookies for search = %@", [sharedHTTPCookieStorage cookiesForURL:searchURL]);
    NSLog(@"All Cookies = ");
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
        NSLog(@"%@", cookie);
    }
    /*
    Console Output (With URL and cookie values hidden):
    Cookies for search = ()
    All Cookies = 
    <NSHTTPCookie version:0 name:"auth_token" value:"COOKIEVALUE" expiresDate:(null) created:2001-01-01 00:00:01 +0000 (1) sessionOnly:TRUE domain:"<search_url>" path:"/" isSecure:FALSE>
    <NSHTTPCookie version:0 name:"auth_token" value:"COOKIEVALUE" expiresDate:(null) created:2012-05-14 17:59:36 +0000 (3.58711e+08) sessionOnly:TRUE domain:"<URL_of_auth_cookie>" path:"/" isSecure:FALSE>
    */
4

1 回答 1

0

如果您在新 cookie 中提供的值足够好,则必须保持标签,因为根据 Apple 参考,如果初始化值以任何特定方式无效,它应该返回 nil。您可以在此处的此链接中进一步查看。它有一种从头开始创建 cookie 的好方法!

于 2012-05-28T13:26:50.837 回答