Swift / iOS. This is an extention of the WKWebView that will allow the searching of a web page for a specific string.
It will hilight in yellow ( but you can change that in the JS code) all occurances of a search string in a UIWebView. Next, if you are using the previous/next features it will scroll to the next/previous occurance and hilight it in green ( again, you can change that color as well in the JS)
SearchWebView.js SearchWKWebView.swift
The WKWebView extention methods are pretty self explanitory
func highlightAllOccurencesOfString(string:String)
func handleSearchResultCount( completionHandler: @escaping (_ count:Int) -> Void )
func removeAllHighlights()
func searchNext()
func searchPrevious()
Pretty straight forward except the handleSearchResultCount is a bit tricky. Because in Swift, when calling javascript in code it is done on a seperate thread, you will have to create a completion handler closure to get notified on when this call is completed...
The code here has a great example of how to do this in the view controller.
//get the count
//define completion handler closure for the count
let countCompletionHandler: (Int) -> Void = {
self.occurancesLabel.text = "\($0)"
print("count found : \($0)")
}
//get the count
webView.handleSearchResultCount( completionHandler: countCompletionHandler )
Thats it!!! Pretty straight forward mothods to extend and use on WKWebView
Setting the hilight colors......
MIT open source...
If you found it useful and it saved you time and effort, please donate... Thank you!