2012-05-14 76 views
0

我想從NSHTTPCookieStorage中取出一個cookie,並將其用於我的應用中的另一個URL。我從NSHTTPCookieStorage複製一個cookie,然後使用複製的cookie在NSHTTPCookieStorage中設置不同URL的cookie。然而,當我打印出其他網址的cookie時,我正在嘗試關注代碼。在NSHTTPCookieStorage中設置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> 
    */ 

回答

0

你必須保持標籤,如果因爲按照蘋果的參考,它應該返回nil,如果初始化值在任何特定的方式無效,你在新的餅乾提供的值是不夠好。 您可以在此處查看此處link。它從頭開始創建一個相當不錯的方式!