Cap-go / capacitor-inappbrowser

Capacitor plugin in app browser with urlChangeEvent
MIT License
71 stars 52 forks source link

clearCookies()is not clearing the cache and cookies on iOS #173

Open iniziokochi opened 3 months ago

iniziokochi commented 3 months ago

I am using the plugin to load the login webpage in my app:

Application is opening the login url with method InAppBrowser.openWebView().

Before opening the web view I am calling InAppBrowser.clearCookies({url: authUrl, cache: true})

Once the login is success I am calling the InAppBrowser.close() methods and clearing all browser listeners by invoking the method InAppBrowser.removeAllListeners() from the “closeEvent” listener method.

But the clearCookies() is not clearing the cache and cookies in the iPhone devices. As the cookies and cache are not clearing properly, the session is not cleaned up and user is able to enter the app without login.

ionic 8.x Angular 16/18 capacitor 5.x iOS 15+

Anthony-Matheou commented 3 months ago

I have the same use case as you where I need the cookies/cache to clear for an authentication flow in iOS. I was able to get them to clear by changing the clearCookies function in the InAppBrowserPlugin.swift file to the following:

 @objc func clearCookies(_ call: CAPPluginCall) {
        let dataStore = WKWebsiteDataStore.default()
        let clearCache = call.getBool("cache") ?? false

        DispatchQueue.main.async {
            Task {
                if clearCache {
                    let cacheTypes: Set<String> = [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache, WKWebsiteDataTypeOfflineWebApplicationCache]
                    let caches = await dataStore.dataRecords(ofTypes: cacheTypes)
                    await dataStore.removeData(ofTypes: cacheTypes, for: caches)
                }

                let cookies = await dataStore.dataRecords(ofTypes: [WKWebsiteDataTypeCookies])
                await dataStore.removeData(ofTypes: [WKWebsiteDataTypeCookies], for: cookies)

                call.resolve()
            }
        }
    }

Not sure if this solution matches what the developers of this plugin intend this function to do, so I don't know if they will want to implement this change. But it works for what I need it to do, and sounds like it should work for you as well.

iniziokochi commented 3 months ago

The workaround you provided to fix the clearCookie function issue on iOS is greatly appreciated, @Anthony-Matheou . Let me attempt the same thing.

riderx commented 2 months ago

Ok the clearCookie was a big mess since a while. I create a clearAllCookie and clearCache method and refactored clearCookie. Then it will be easier for us to debug and improve please try 6.5.1