JS interactively clicks on the picture in WKWebView and previews the example

小云云
Release: 2018-01-08 09:09:11
Original
1854 people have browsed it

This article mainly introduces JS interactive clicking on pictures in WKWebView to achieve preview effects. Friends who need it can refer to it. I hope it can help everyone.

  • Swift 4.0

  • WKWebView

1. Inject js code (emphasis)

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { let jsGetImages = "function getImages(){" + "var objs = document.getElementsByTagName(\"img\");" + "var imgScr = '';" + "for(var i=0;i
        
Copy after login

2. Use the SKPhotoBrowser framework to implement the image preview function

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { let requestString = navigationAction.request.url?.absoluteString print(requestString!) if (requestString?.hasPrefix("image-preview"))!{ let imgUrl = NSString.init(string: requestString!).substring(from: "image-preview:".count ) let index = imgUrlArray.index(of: imgUrl) let browser = SKPhotoBrowser(photos: images) browser.initializePageIndex(index) present(browser, animated: true, completion: {}) } decisionHandler(.allow) //一定要加上这句话 }
Copy after login

Related recommendations:

IOS uses WKWebView to load videos and reports an error Code=204

IOS Html rich text rendering method: memory usage comparison of DTCoreText, WKWebView, and UIWebView_html/css_WEB-ITnose

Detailed explanation of WebView knowledge points

The above is the detailed content of JS interactively clicks on the picture in WKWebView and previews the example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!