UIWebView和WKWebView设置cookie

首先是UIWebView的设置cookie

- (void)setCookie{
    NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
    //这里需要注意的一点是必须要加上设置NSHTTPCookieDomain 和 NSHTTPCookiePath是要加载的URL的host和path,不然的话cookie会设置不成功的,缺一不可
    [cookieProperties setObject:[self.url host] forKey:NSHTTPCookieDomain];
    [cookieProperties setObject:[self.url path] forKey:NSHTTPCookiePath];
    [cookieProperties setObject:@"useId" forKey:NSHTTPCookieName];
    [cookieProperties setObject:@"holdfun" forKey:NSHTTPCookieValue];
    NSHTTPCookie *cookieuser = [NSHTTPCookie cookieWithProperties:cookieProperties];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookieuser];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
    NSURLRequest *request = [NSURLRequest requestWithURL:self.url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
}

删除cookie

- (void)deleteCookie{
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookieAry = [cookieJar cookiesForURL: self.url];
    for (cookie in cookieAry) {
        [cookieJar deleteCookie: cookie];
    }  
}

UIWebView向上面那样设置就可以设置好cookie了,而且H5端也可以获取得到,但是WKWebView不行,WKWebView实例不会把Cookie存入到App标准的的Cookie容器(NSHTTPCookieStorage)中。
因为NSURLSession/NSURLConnection等网络请求使用NSHTTPCookieStorage进行访问Cookie,所以不能访问WKWebView的Cookie,现象就是WKWebView存了Cookie,其他的网络类如NSURLSession/NSURLConnection却看不到。NSHTTPCookieStorage 是实现管理cookie的单利,每个cookie都是NSHTTPCookie类的实例,做为一个规则,cookie在所有应用 之间共享并在不同进程之间保持同步。与Cookie相同的情况就是WKWebView的缓存,凭据等。WKWebView都拥有自己的私有存储,因此和标准cocoa网络类兼容的不是那么好。需要使用js注入的方法设置UIWebView的cookie,下面看看代码。

-(void)loadWKWebView
{
    self.url = [NSURL URLWithString:@"http://edu.holdfun.cn/news/"];
    NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
    [cookieProperties setObject:@"edu.holdfun.cn" forKey:NSHTTPCookieDomain];
    [cookieProperties setObject:@"/news" forKey:NSHTTPCookiePath];
    [cookieProperties setObject:@"useId" forKey:NSHTTPCookieName];
    [cookieProperties setObject:@"holdfun" forKey:NSHTTPCookieValue];
    NSHTTPCookie *cookieuser = [NSHTTPCookie cookieWithProperties:cookieProperties];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookieuser];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
    NSURLRequest *request = [NSURLRequest requestWithURL:self.url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    NSString *cookieStr = [self setJSCookie];
    WKUserScript *cookieScipt = [[WKUserScript alloc] initWithSource:cookieStr injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
    WKUserContentController *wku = [[WKUserContentController alloc] init];
    [wku addUserScript:cookieScipt];
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    wkWebConfig.userContentController = wku;
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:wkWebConfig];
    [webView loadRequest:request];
    [self.view addSubview:webView];
    webView.UIDelegate = self;
    webView.navigationDelegate = self;
}

-(NSString *)setJSCookie{
    NSData * cookieData = [[NSUserDefaults standardUserDefaults]objectForKey:@"cookieData"];
    NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookieData];
    for (NSHTTPCookie *cookie in cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
    NSMutableDictionary *cookieDic = [NSMutableDictionary dictionary];
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
        [cookieDic setObject:cookie.value forKey:cookie.name];
    }
    NSString *cookieStr = @"";
    //这里可以筛选自己想要的cookie注入就行,也可以删掉全部注入
    for (NSString *key in cookieDic) {
        if ([key isEqualToString:@"useId"]) {
            NSString *appendString = [NSString stringWithFormat:@"'%@=%@;path=/';",key,[cookieDic valueForKey:key]];
            cookieStr = [NSString stringWithFormat:@"%@document.cookie=%@",cookieStr,appendString];
        }
    }
    return cookieStr;
}

你可能感兴趣的:(UIWebView和WKWebView设置cookie)