4

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.

ji sung Choi
  • 123
  • 1
  • 7
  • Hey, wondering if you figures this out? I am having the same issue, after login when switching to another webview the sessions disappear or reset or something on physical devices but not on the xcode sims – pilotman Jun 16 '21 at 20:05
  • No, I didn’t get to resolve this issue. – ji sung Choi Jun 17 '21 at 21:06
  • If you are interested I came up with a simple solution, if so I will answer below – pilotman Jun 17 '21 at 23:01
  • i am also facing this issue, have any one found a solution. i have tried this https://github.com/Kofktu/WKCookieWebView but it doesnt seems to work for me – Muhammad Ahmed May 25 '22 at 11:12

0 Answers0