Setting Cookies in WKWebView


#1

I am using the following code to set the cookies to WKWebview. For the first time when app launches, webview is working fine and able to see the page. But when i click on next times, some times it works and some times it doesn’t. I spent numerous hours on that but no luck. Your help is greatly appreciated.


#2

webview.on(WebView.loadStartedEvent, function loadStart(args: LoadEventData) {
const sharedCookieInstance: any = NSHTTPCookieStorage.sharedHTTPCookieStorage;
const cookies: any = sharedCookieInstance.cookies;
for (let i = 0; i < cookies.count; i++) {
const nsCookie: any = cookies[i];
console.log(“Name: " + nsCookie.name + " Domain: " + nsCookie.domain);
if (nsCookie.domain === “m.intermountain.net” ||
nsCookie.domain === “adfs.intermountain.net” ||
nsCookie.domain === “search.intermountain.net”) {
const dic: any = new NSDictionary([nsCookie.name, nsCookie.value, nsCookie.domain,”/"],
[NSHTTPCookieName, NSHTTPCookieValue,NSHTTPCookieDomain,NSHTTPCookiePath]);
const cookie: any = new NSHTTPCookie(dic);
webview.ios.configuration.websiteDataStore.httpCookieStore.getAllCookies(function(cookies1){
console.log(“Cookie retrieved successfully”,cookies1.count);

            webview.ios.configuration.websiteDataStore.httpCookieStore.setCookieCompletionHandler(cookie, function(){
                console.log("Cookie set successfully");
                
            });
            
        });