WKWebView中删除Cookie crash

removeCookiesSinceDate crash

- (void)removeCookiesSinceDate:(NSDate *)date API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));

删除所有的Cookie支持iOS8以上版本,但是用的手机是iOS8 crash了,所以只能通过手动删除

- (void)clearWKCookies
{
    //删除NSHTTPCookieStorage中的cookies
    if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_9_0) {
        // iOS 9.0 以上系统的处理
        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:PAWKCookiesKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } else {
        // iOS 9.0 以下系统的处理
        NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
        for (NSHTTPCookie *cookie in cookies) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
        }
    }
    
}

你可能感兴趣的:(WKWebView中删除Cookie crash)