iOS WKWebView查找字符串未选择和滚动

iOS WKWebView查找字符串未选择和滚动,ios,swift,webkit,wkwebview,Ios,Swift,Webkit,Wkwebview,由于iOS 14WebKit支持,但还没有任何文档 然而,在WWDC会话中,他们提到这是“在页面上查找”的一项基本功能,在该功能中,您可以查找字符串,WebView将选择该字符串并滚动至其中心 当我得到matchFoundtrue的结果时,它似乎很容易使用和查找字符串,但是没有选择,也没有滚动。也许我错过了什么 这是我尝试过的代码: let webView = WKWebView() // ... // after loading a website with the desired

由于iOS 14WebKit支持,但还没有任何文档

然而,在WWDC会话中,他们提到这是“在页面上查找”的一项基本功能,在该功能中,您可以查找字符串,WebView将选择该字符串并滚动至其中心

当我得到
matchFound
true
的结果时,它似乎很容易使用和查找字符串,但是没有选择,也没有滚动。也许我错过了什么

这是我尝试过的代码:

let webView = WKWebView()
    
// ...
// after loading a website with the desired string on it.
// ...
    
webView.find("hello world") { result in
    print(result.matchFound) // true
}

到目前为止,我只能结合一点JavaScript让它“有点工作”

let webView = WKWebView()
webView.select(nil)
webView.find("hello world") { result in
    guard result.matchFound else { return }
    webView.evaluateJavaScript(
        "window.getSelection().getRangeAt(0).getBoundingClientRect().top") { offset, _ in
        guard let offset = offset as? CGFloat else { return }
        webView.scrollView.scrollRectToVisible(
            .init(x: 0,
                  y: offset + webView.scrollView.contentOffset.y,
                  width: 100,
                  height: 100), animated: true)
    }
}
说明:

一,。
webView。选择(nil)
使其成为第一响应者。 这一点很重要,否则当找到匹配项时,将不会选择它

二,。
webView.find(“我的字符串”)

三,。 如果找到匹配项,请使用JavaScript获取所选文本的偏移量

四,。
当收到偏移量时,滚动到它。

我还发现,在
webView.find(“hello world”)
之前调用
webView.select(nil)
会启用选择,但仍然没有滚动。