本來以為自定義cookie能夠把一個域中的cookies,帶給另外一個域來使用.實際中沒有考慮到服務器也是根據域進行邏輯限定的,最終無法使用的.
下面的代碼當作證明過程中的一個關鍵部分實現吧.
NSArray *tmp_Cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"http://www.thirtydevs.com"]]; NSLog(@"1:%@",[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]); for (NSHTTPCookie *cookie in tmp_Cookies) { //從cookies中獲取sessionid並保存. if ([[cookie domain] isEqualToString:@"www.thirtydevs.com"]) { NSDictionary *cookieProperties = [[NSMutableDictionary alloc]init]; [cookieProperties setValue:[cookie value] forKey:NSHTTPCookieValue]; [cookieProperties setValue:[cookie name] forKey:NSHTTPCookieName]; [cookieProperties setValue:@"m.thirtydevs.com" forKey:NSHTTPCookieDomain]; //沒有增加新cookie也許是由於沒有把NSHTTPCookieExpires和NSHTTPCookiePath設置好. [cookieProperties setValue:nil forKey:NSHTTPCookieExpires]; [cookieProperties setValue:[cookie path] forKey:NSHTTPCookiePath]; NSHTTPCookie *ncookie = [[NSHTTPCookie alloc] initWithProperties:cookieProperties]; [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:ncookie]; [ncookie release]; break; } } NSLog(@"2:%@",[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]);