2013年1月18日 星期五

cocoa 取得 webview cookie

在 iOS app 中

可以使用 NSHTTPCookie 取得 同一 app 中 webview 的 cookie 值

NSHTTPCookie *cookie;

NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];

for (cookie in [cookieJar cookies]) {

NSLog(@"%@", cookie);

}

其他方法

NSMutableString *cDesc = [[[NSMutableString alloc] init] autorelease];

[cDesc appendString:@"[NSHTTPCookie]\n"];

[cDesc appendFormat:@" name = %@\n", [[cookie name] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[cDesc appendFormat:@" value = %@\n", [[cookie value] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[cDesc appendFormat:@" domain = %@\n", [cookie domain]];

[cDesc appendFormat:@" path = %@\n", [cookie path]];

[cDesc appendFormat:@" expiresDate = %@\n", [cookie expiresDate]];

[cDesc appendFormat:@" sessionOnly = %d\n", [cookie isSessionOnly]];

[cDesc appendFormat:@" secure = %d\n", [cookie isSecure]];

[cDesc appendFormat:@" comment = %@\n", [cookie comment]];

[cDesc appendFormat:@" commentURL = %@\n", [cookie commentURL]];

[cDesc appendFormat:@" version = %@\n", [cookie version]];

實作時,value 可能會有 %00

可以用 [nsstring componentsSeparatedByString:@“%00”] 方法分割陣列

然後用 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 還原數值

沒有留言: