你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS中增加新的cookie

iOS中增加新的cookie

編輯:IOS開發綜合

本來以為自定義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]);

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved