Reputation: 1657
I am trying to add an WKWebView to my app. Following the documentation, I implemented this:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "www.myurl.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
Now my Problem: The website I try to access needs an authentication. In safari I get asked for. But my app just presents The error "404 - this website doesnt exist". After searching I got the information that receiving a URLAuthenticationChallenge could solve the problem. With documentation and information from the internet, I added the following code:
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
print("test if pass")
let user = "*****"
let password = "*****"
let credential = URLCredential(user: user, password: password, persistence: URLCredential.Persistence.forSession)
challenge.sender?.use(credential, for: challenge)
}
I added the print statement to see if I get into my code. But after starting my app there is still the 404 error and nothing was printed to my console.
Can someone help me how I can add authentication? Thanks :)
Upvotes: 1
Views: 1767
Reputation: 2283
Update your line,
let myURL = URL(string: "www.myurl.com")
with
let myURL = URL(string: "http://username:[email protected]/)
This help me for same issue as your's. Hope this help you too.
Upvotes: 2