WKWebView function for detecting if the URL has changed
What do you mean they don't always seem to fire? What kind of elements? They have to in order for the WkWebView to work.
Your first indication that the URL is trying to change is in: decidePolicyForNavigationAction
- (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (WKNavigationAction *) navigationAction decisionHandler: (void (^)(WKNavigationActionPolicy)) decisionHandler {
NSLog(@"%s", __PRETTY_FUNCTION__);
decisionHandler(WKNavigationActionPolicyAllow); //Always allow
NSURL *u1 = webView.URL;
NSURL *u2 = navigationAction.request.URL; //If changing URLs this one will be different
}
By the time you get to: didStartProvisionalNavigation It has changed.
- (void) webView: (WKWebView *) webView didStartProvisionalNavigation: (WKNavigation *) navigation {
NSLog(@"%s", __PRETTY_FUNCTION__);
NSURL *u1 = webView.URL; //By this time it's changed
}
All you'd have to do is implement these delegate methods (in Swift) and do what you want when you see it change.
Issue with observing WKWebView URL changes via JavaScript events
Before you go any further, I would strongly recommend that you use the modern Swift method of observing:
var webViewURLObserver: NSKeyValueObservation?
override func viewDidLoad() {
// ...
webViewURLObserver = webview.observe(\.url, options: .new) { webview, change in
print("URL: \(String(describing: change.newValue))")
// The webview parameter is the webview whose url changed
// The change parameter is a NSKeyvalueObservedChange
// n.b.: you don't have to deregister the observation;
// this is handled automatically when webViewURLObserver is dealloced.
}
}
Doing this makes your code much cleaner and easier to reason about while you're debugging the issue.
Detect text selection changes in UIWebView text selection
You can use document.addEventListener("selectionchange")
which fires whenever the selection changes, and forward those changes to Obj-C through a bridge.
Bridges:
- WebViewJavascriptBridge
- JavaScriptCore
Example Code:
document.addEventListener("selectionchange", function(){
bridge.send("selectionchange"); // WebViewJavaScriptBridge
bridge.selectionChanged(); // JavaScriptCore
}, false);
SwiftUI WKWebView detect url changing
I have found a very good solution to my question. I will post it here. Maybe someone wants to see it and might be useful to them.
observe.observation = uiView.observe(\WKWebView.url, options: .new) { view, change in
if let url = view.url {
// do something with your url
}
}
Listen for URL fragment id change in WebView
As mitvenkatesan described in his comment to the question, this can be achieved by overriding the window.onhashchange() event of the page inside the WebView.
It turns out that this solution has already been addressed in another question.
Android detect webview URL change
I know I'm late to the game but I ran into this issue over and over ... I finally found a sloution which is pretty straight forward. Just override WebViewClient.doUpdateVisitedHistory
override fun doUpdateVisitedHistory(view: WebView?, url: String?, isReload: Boolean) {
// your code here
super.doUpdateVisitedHistory(view, url, isReload)
}
It works with all url changes even the javascript ones!
If this does not make you happy then I don't know what will :)
How can I load file with hash with WKWebview
Construct URL from string work as expected:
let path = "www/index.html"
let page = "#/some-hash-path"
guard let parsedUrl = URL(string: Bundle.main.bundleURL.absoluteString + path + page) else {
return
}
self.webView.loadFileURL(parsedUrl, allowingReadAccessTo: Bundle.main.bundleURL)
How to change URL hash of WebView in Swift
Hey i resolve my problem with this code:
On Swift (Native Code) i created the timer listen javascript variable change:
override func viewDidLoad() {
super.viewDidLoad()
...
var timer = NSTimer.scheduledTimerWithTimeInterval(
0.4,
target: self,
selector: #selector(ViewController.update), //#call method
userInfo: nil,
repeats: true)
Use this method (#call method) to listen the javascript variable:
var pathCurrent: String!;
func update() {
let path: String! = webView.stringByEvaluatingJavaScriptFromString("var path = public.path; (function(_path){return _path;})(path)")
if (path != pathCurrent)
{
//path changed
print(path);
pathCurrent = path;
}
print(path);
}
}
Note, this code, read the public variable ->
var path = public.path;
Now, in Angular JS, you can change the public variable when route was changed:
$scope.$on("$routeChangeSuccess", function($currentRoute, $previousRoute) {
public.path = $location.$$path;
});
Or you can detect via JavaScript:
window.onhashchange = function() {
public.path = location.href.substr(path.indexOf("#/")+1);
}
now you know when angular path was changed in your native swift code :D
Related Topics
How to Create a Circle with Calayer
Table View Cells Changing Colors When Scrolling Swift
How to Detect If The User Was Deleted from Firebase Auth
Convert/Wrap Swift Struct as Nsvalue for Caanimation Purposes
Detect When a Custom Cell Is Selected from Within The Cell Itself
Apple Turicreate Always Return The Same Label
Facebook Graphrequest for Swift 5 and Facebook Sdk 5
Does Swift Allow Code Blocks Without Conditions/Loops to Reduce Local Variable Scope
Swiftycam Capture Session Is Not Running
Sizing a UIpickerview Inside a UIalertview
How Is Commoncrypto Used in Swift3
Crash Casting Wknsurlrequest As? Other Type
Macos Security Scoped Url Bookmark for Folder
Get Path of a File in a Data Set Located in Assets.Xcassets
Include Dictionary or Array Value in Swift String with Backslash Notation
Why This Line Is Not Covered? Xcode Code Coverage
How to Override Setter in Swift
A Concise Way to Not Execute a Loop Now That C-Style for Loops Are Going to Be Removed from Swift 3