【问题标题】:WKWebView AuthorizationWKWebView 授权
【发布时间】:2016-10-21 13:14:54
【问题描述】:

我想打开一个网站,在 WKWebView 中使用密码/用户名(基本)授权。
为此,我使用方法didReceiveAuthenticationChallenge

import UIKit
import WebKit

class ViewController: UIViewController {

@IBOutlet weak var container: UIView!
var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    webView = WKWebView()
    container.addSubview(webView)
    let urlStr = "https://vplan.bielefeld-marienschule.logoip.de/subst_002.htm"
    let url = NSURL(string: urlStr)!
    let request = NSURLRequest(url: url as URL)
    webView.load(request as URLRequest)
}
func webView(webView: WKWebView, didReceiveAuthenticationChallenge challenge: URLAuthenticationChallenge, completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void){
    if challenge.protectionSpace.host == "https://vplan.bielefeld-marienschule.logoip.de/subst_002.htm" {
        let userStr = "********"
        let passwordStr = "********"
        let credential = URLCredential(user: userStr, password: passwordStr, persistence: URLCredential.Persistence.forSession)
        challenge.sender?.use(credential, for: challenge)
        completionHandler(.useCredential, credential)
    }
}

override func viewDidAppear(_ animated: Bool) {
    let frame = CGRect(x: 0, y: 0, width: container.bounds.width, height: container.bounds.height)
    webView.frame = frame

但由于某种原因,它不起作用。
添加代码行

completionHandler(URLSession.AuthChallengeDisposition.UseCredential, credential)

就像这里的描述:Authentication with WKWebView in Swift 产生了一个错误。
我还尝试将challenge.protectionSpace.host 更改为vplan.bielefeld-marienschule.logoip.de:443,在Safari pop-up 中显示为登录地址,并更改为服务器的IP 地址(93.206.31.253)。

希望你能帮助我。 谢谢!

【问题讨论】:

    标签: ios swift wkwebview


    【解决方案1】:

    在您的代码中,您尚未将 Web 视图的导航委托设置为您的控制器。

    将控制器的定义更改为:

    class ViewController: UIViewController, WKNavigationDelegate
    

    创建 Web 视图后,将导航委托设置为您的控制器:

    webView.navigationDelegate = self
    

    根据您是否使用 Swift 3,您可能需要将方法的签名更改为 webView(_:didReceive:completionHandler:)

    【讨论】:

    • 不幸的是,您的最后一个提示导致另一个错误:线程 1:AppDelegate.swift 类中的信号 SIGABART。
    • SIGABART 是一个异常——异常说明了什么?
    • libc++abi.dylib:以 NSException 类型的未捕获异常终止
    • 是的,我想那么多。什么是异常消息?如果看不到,放一个异常断点,当在栈顶行时,在调试器中输入po $arg1
    • 错误:无法实现:无法读取寄存器 rdi 的值 错误:DoExecute 出错,无法 PrepareToExecuteJITExpression
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2018-11-21
    • 2011-08-05
    • 1970-01-01
    • 2013-11-01
    • 2016-12-17
    • 1970-01-01
    相关资源
    最近更新 更多