[英]Username/password authentication in WKWebView
在Safari中訪問網站時,它將要求提供用戶名和密碼。
我出於某種原因試圖將網站包裝到應用程序中,但是無法啟動用戶名/密碼提示。
這是我到目前為止的代碼:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let user = "*****"
let password = "*****"
let credential = URLCredential(user: user, password: password, persistence: URLCredential.Persistence.forSession)
challenge.sender?.use(credential, for: challenge)
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "http://WebsiteName")!
let myRequest = URLRequest(url: myURL)
webView.load(myRequest)
}
}
我希望有一個用戶名/登錄提示框,但它只是沒有提示的401錯誤。
我幾年前用webView做過類似的事情,也許這會有所幫助
webView.delegate = self
guard let orderID = orderID else { return }
let urlString = GET_PAYMENT_PAGE + "\(orderID)" + "&comments=" + DataManager.shared.peymentComment
guard let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!) else { return }
var request = URLRequest(url: url)
let username = Authentication.auth.phoneNumber
let password = Authentication.auth.token
let loginString = String(format: "%@:%@", username, password)
let loginData = loginString.data(using: String.Encoding.utf8)!
let base64LoginString = loginData.base64EncodedString()
request.httpMethod = "POST"
request.allHTTPHeaderFields = JSON_HEAD
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
webView.loadRequest(request)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.