Posted at

UIWebViewにCookieを設定する方法

More than 5 years have passed since last update.

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]を使って設定することが出来ます。

引数に渡すcookieの生成方法などはサンプルを参照。


SetCookieSample


@implementation SetCookieSample

- (void)cookieSample:(NSString *)cookieName cookieValue:(NSString *)cookieValue cookieDomain:(NSString *)cookieDomain {
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:
cookieName, NSHTTPCookieName,
cookieValue, NSHTTPCookieValue,
cookieDomain, NSHTTPCookieDomain,
@"/", NSHTTPCookiePath,
[NSDate distantFuture], NSHTTPCookieExpires,
nil]];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}

@end