SwiftStudier
SwiftStudier

Reputation: 2324

Clear webView cookies (Swift)

How can I clear all cookies for few hosts in my webView? Method from this question not working.

I'm trying to work with vk.com oauth and it does not have user-logout method, so deleting cookis is one of existing ways to make other user possible to log in

Answer is below

Upvotes: 15

Views: 20484

Answers (7)

Syed Faizan Ahmed
Syed Faizan Ahmed

Reputation: 544

Nothing above worked for me. Using the following:

  • Swift 5
  • Xcode 12.4
  • WebKit

Below is the solution worked for me:

func removeCookies() {
    HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
    print("All cookies deleted")

    WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
        records.forEach { record in
            WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
            print("Cookie ::: \(record) deleted")
        }
    }
}

Upvotes: 11

Tim Hazhyi
Tim Hazhyi

Reputation: 171

Variation to remove based on date. .distantPast to remove all

HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)

Upvotes: 2

Andrey M.
Andrey M.

Reputation: 3069

UIWebView is nearly deprecated. So for WKWebView instance make this code:

let cookieStore = webView.configuration.websiteDataStore.httpCookieStore

cookieStore.getAllCookies {
    cookies in

    for cookie in cookies {
        cookieStore.delete(cookie)
    }
}

Upvotes: 3

SwiftStudier
SwiftStudier

Reputation: 2324

I made it by

        let cookieJar = NSHTTPCookieStorage.sharedHTTPCookieStorage()

        for cookie in cookieJar.cookies! {
           // print(cookie.name+"="+cookie.value)
            cookieJar.deleteCookie(cookie)
        }

Swift 4

func removeCookies(){
    let cookieJar = HTTPCookieStorage.shared

    for cookie in cookieJar.cookies! {
        cookieJar.deleteCookie(cookie)
    }
}

Upvotes: 14

Saleh Enam Shohag
Saleh Enam Shohag

Reputation: 1119

By avoiding Force Unwrap

private func clearCookie2(){
    let storage = HTTPCookieStorage.shared
    if let cookies = storage.cookies{
        for cookie in cookies {
            storage.deleteCookie(cookie) 
        }
    }
}

And if you want to delete only specific cookie

let DOMAIN_NAME = "abc.com"
private func clearCookie(){
    let storage = HTTPCookieStorage.shared
    if let cookies = storage.cookies{
        for cookie in cookies {
            if(cookie.domain.contains(DOMAIN_NAME)){
                storage.deleteCookie(cookie)
            }
        }
    }
}

Upvotes: 1

Chris
Chris

Reputation: 1539

One liner in Swift 5

HTTPCookieStorage.shared.cookies?.forEach(HTTPCookieStorage.shared.deleteCookie)

Upvotes: 12

Kerim Khasbulatov
Kerim Khasbulatov

Reputation: 722

swift 4

func removeCookies(){
    let cookie = HTTPCookie.self
    let cookieJar = HTTPCookieStorage.shared

    for cookie in cookieJar.cookies! {
        cookieJar.deleteCookie(cookie)
    }
}

Upvotes: 5

Related Questions