关于H5中标签设置_blank跳转问题
_blank代表打开一个新的窗口这个问题如果默认不去处理,那么点击h5是无效的(不会触发页面加载),如何处理这个问题,请看代码
/// 注意看api注释
/** @abstract The frame requesting the navigation.
*/
@NSCopying open var sourceFrame: WKFrameInfo { get }
/** @abstract The target frame, or nil if this is a new window navigation.
*/
@NSCopying open var targetFrame: WKFrameInfo? { get }
/// 很明确了,通过targetFrame判断下一步的导航事件
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
return WKNavigationActionPolicy.allow
}
/// 以上应该只是处理的一些比较特殊的情况,h5中的交互需要打开新的window
请求拦截
/// navigationResponse可以拦截处理一些特殊情况,比如statusCode 404的情况
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse) async -> WKNavigationResponsePolicy {
let response:HTTPURLResponse = navigationResponse.response as! HTTPURLResponse
if response.statusCode == 404 {
/// TODO...
}
return WKNavigationResponsePolicy.allow
}
关于swift KVO写法
/// 必须创建一个强引用的成员属性
private var progressObservation: NSKeyValueObservation?
/// 添加监听
progressObservation = webView.observe(\.estimatedProgress, changeHandler: { _, _ in
})