How to delete WKWebview cookies

后端 未结 13 1492
北荒
北荒 2020-11-27 04:18

For now I am doing like this

    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [         


        
13条回答
  •  独厮守ぢ
    2020-11-27 04:57

    Building on top of all the existing answers, if you are trying to clear cookies and data records for a specific WKWebView instance 'webView' and not the 'default' stored cookies and data records, you could use the following:

    let dataStore = webView.configuration.websiteDataStore
    let cookieStore = dataStore.httpCookieStore
    cookieStore.getAllCookies {
        $0.forEach { cookie in
            cookieStore.delete(cookie)
        }
    }
    dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
        records.forEach { record in
            dataStore.removeData(ofTypes: record.dataTypes, for: [record]) { }
        }
    }
    

提交回复
热议问题