iOS 使用WKWebView缓存cookie

废话不多说,直接上代码

 

//插入设置cookie

-(void)insertCookie{

    // 创建一个可变字典存放cookie

    NSHTTPCookie *cookie =[self setcookie];

    if (@available(iOS 11.0, *)) {

        WKHTTPCookieStore *cookieStore = self.webView.configuration.websiteDataStore.httpCookieStore;

        [cookieStore setCookie:cookie completionHandler:nil];

    }

    

    NSHTTPCookieStorage * shareCookie = [NSHTTPCookieStorage sharedHTTPCookieStorage];

    [shareCookie setCookie:cookie];

    

    NSMutableArray *TempCookies = [NSMutableArray array];

    NSMutableArray *localCookies =[NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"webCookie"]];

    for (int i = 0; i < localCookies.count; i++) {

        NSHTTPCookie *TempCookie = [localCookies objectAtIndex:i];

        if ([cookie.name isEqualToString:TempCookie.name] &&

            [cookie.domain isEqualToString:TempCookie.domain]) {

            [localCookies removeObject:TempCookie];

            i--;

            break;

        }

    }

    [TempCookies addObjectsFromArray:localCookies];

    [TempCookies addObject:cookie];

    NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: TempCookies];

    [[NSUserDefaults standardUserDefaults] setObject:cookiesData forKey:@"webCookie"];

    [[NSUserDefaults standardUserDefaults] synchronize];

}

//清空所有cookie

 

- (void)clearWKCookies

{

    if (@available(iOS 11.0, *)) {

        NSSet *websiteDataTypes = [NSSet setWithObject:WKWebsiteDataTypeCookies];

        NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];

        [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{

        }];

    }

    

    //删除NSHTTPCookieStorage中的cookies

    NSHTTPCookieStorage *NSCookiesStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];

    [NSCookiesStore removeCookiesSinceDate:[NSDate dateWithTimeIntervalSince1970:0]];

    

    NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: @[]];

    [[NSUserDefaults standardUserDefaults] setObject:cookiesData forKey:@"webCookie"];

    [[NSUserDefaults standardUserDefaults] synchronize];

}

//删除指定cookie

-(void)deleteCookie{

    NSHTTPCookie *cookie =[self setcookie];

    if (@available(iOS 11.0, *)) {

        

        //删除WKHTTPCookieStore中的cookies

        WKHTTPCookieStore *cookieStore = self.webView.configuration.websiteDataStore.httpCookieStore;

        [cookieStore deleteCookie:cookie completionHandler:nil];

    }

    

    //删除NSHTTPCookieStorage中的cookie

    NSHTTPCookieStorage *NSCookiesStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];

    [NSCookiesStore deleteCookie:cookie];

    

    //删除磁盘中的cookie

    NSMutableArray *localCookies =[NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"webCookie"]];

    for (int i = 0; i < localCookies.count; i++) {

        NSHTTPCookie *TempCookie = [localCookies objectAtIndex:i];

        if ([cookie.domain isEqualToString:TempCookie.domain] &&

            [cookie.domain isEqualToString:TempCookie.domain] ) {

            [localCookies removeObject:TempCookie];

            i--;

            break;

        }

    }

    

    NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: localCookies];

    [[NSUserDefaults standardUserDefaults] setObject:cookiesData forKey:@"webCookie"];

    [[NSUserDefaults standardUserDefaults] synchronize];

}

-(NSHTTPCookie*)setcookie{

    NSMutableDictionary *fromappDict = [NSMutableDictionary dictionary];

    [fromappDict setObject:@"fromapp" forKey:NSHTTPCookieName];

    [fromappDict setObject:@"ios" forKey:NSHTTPCookieValue];

    // kDomain和NSHTTPCookieOriginURL我是指定需要保存的ip

    [fromappDict setObject:@"http://www.baidu.com" forKey:NSHTTPCookieDomain];

    [fromappDict setObject:@"http://www.baidu.com" forKey:NSHTTPCookieOriginURL];

    [fromappDict setObject:@"/" forKey:NSHTTPCookiePath];

    [fromappDict setObject:@"0" forKey:NSHTTPCookieVersion];

    // 将可变字典转化为cookie

    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:fromappDict];

    return cookie;

}

 

最后就是调用了,如图所示,将cookie添加到webview的configuration,最后webview加载request即可iOS 使用WKWebView缓存cookie_第1张图片

内容有借鉴PAWebView,传送门 https://github.com/llyouss/PAWebView

你可能感兴趣的:(iOS,WKWebView)