0

I want to embed a website to my app with wkwebview using SwiftUI. Web that I want to use has basic auth prompt credential. I have tried with this code. But the web site still show unauthorized credential.

let url: String

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

func updateUIView(_ webView: WKWebView, context: UIViewRepresentableContext<WebEmbed>) {
    
}

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

    let username = HelperController().getSession().username
    let password = HelperController().getSession().password
    let credential = URLCredential(user: username, password: password, persistence: URLCredential.Persistence.forSession)
    challenge.sender?.use(credential, for: challenge)
    completionHandler(URLSession.AuthChallengeDisposition.useCredential, credential)
}

this is the result of the embed of the website which unauthorized enter image description here

I have checked on the terminal but there is no error. Is there any solution how to fix the credential so can be passed to the website embedded?

Ruli
  • 2,592
  • 12
  • 30
  • 40
  • 2
    If `HelperController` is a SwiftUI view then you use it incorrectly. You need to inject it arguments and then pass to coordinator to be used in auth callback, like in https://stackoverflow.com/a/58825642/12299030. – Asperi Jan 14 '22 at 09:21
  • @Asperi even i use string like example let username = "tes" and let password = "tes" the credential still not working and still unauthorized. any help? – macan tiger Jan 17 '22 at 04:07

0 Answers0