How to keep user logged in on WKWebview in SwiftUI

Viewed 493

I have a website that asks for login in initial page and I would like to have users stay logged in so they don't have to input the login info every time they turn on the app.

Here is my current code for calling WKWebkit. It does everything I need it to do except for the login session keeping part.

import SwiftUI
import WebKit

struct Webview: UIViewRepresentable {

var url: String

func makeUIView(context: Context) -> WKWebView {
    guard let url = URL(string: self.url) else {
        return WKWebView()
    }
    let request = URLRequest(url:url)
    let wkWebview = WKWebView()
    wkWebview.load(request)
    return wkWebview
}

func updateUIView(_ uiView: WKWebView, context:
    UIViewRepresentableContext<Webview>) {
    
    }
}

and I call it on ContentView like

Webview(url: "https://example.com")

I have read developer documentation and searched gist, SO, and apple forum for managing login session/cookie in WKWebview.

Some of the links I have tried implement in my code, but ending at ground zero are below:

WKWebView setting Cookie not possible (iOS 11+)

https://developer.apple.com/forums/thread/99674

https://gist.github.com/skagedal/cd516751e655263218f1cad0281e9941

https://medium.com/dev-genius/how-to-get-cookies-from-wkwebview-and-uiwebview-in-swift-46e1a072a606

https://stackoverflow.com/a/26577303/15047236

I have tried numerous other source codes that I can read/understand what they do for past 2 weeks in vain.

Please help this poor soul out.

0 Answers
Related