Trying to detect change event of the URL being navigated to somewhere else when the user changes the currently viewed webpage. Here is my code.
struct WebView: UIViewRepresentable {
var view = WKWebView()
init() {
print("initialized")
}
func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let newValue = change?[.newKey] as? Int, let oldValue = change?[.oldKey] as? Int, newValue != oldValue {
//Value Changed
print(change?[.newKey])
}else{
//Value not Changed
print(change?[.oldKey])
}
}
func makeUIView(context: UIViewRepresentableContext<WebView>) -> WKWebView {
while(!authenticated) {
}
DispatchQueue.main.async {
var url = URL(string:"https://www.runixcoin.com/")!
if (faceIdSuccess) {
url = URL(string:"https://www.runixcoin.com/dashboard/auth-login-basic.php?face_id=true")!
}
else {
url = URL(string:"https://www.runixcoin.com/dashboard/auth-login-basic.php?return_url=https://www.runixcoin.com/dashboard/dashboard-4.php")!
}
let request = URLRequest(url: url)
view.load(request)
view.addObserver(view, forKeyPath: "URL", options: .new, context: nil)
}
return view
}
func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext<WebView>) {
}
typealias UIViewType = WKWebView
}
The observeValue function is getting called and printing the results I want when the user navigates away from the current page, however the app is crashing... I want to stop the app from crashing. I even tried using the removeObserver code and it either prevented the function from being called or would still crash... What do I need to do?
2022-04-04 18:21:55.489370-0400 Runixcoin[6797:1762100] [error] warning: View context accessed for persistent container runixcoin with no stores loaded
CoreData: warning: View context accessed for persistent container runixcoin with no stores loaded
initialized
Logged in with FaceID
2022-04-04 18:21:58.914721-0400 Runixcoin[6797:1762100] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<WKWebView: 0x106025e00; frame = (0 0; 428 796); layer = <CALayer: 0x283bb1b80>>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: URL
Observed object: <WKWebView: 0x106025e00; frame = (0 0; 428 796); layer = <CALayer: 0x283bb1b80>>
Change: {
kind = 1;
new = "https://www.runixcoin.com/dashboard/auth-password-basic.php";
}
Context: 0x0'
*** First throw call stack:
(0x1811e50fc 0x199a35d64 0x182ad0adc 0x1829a30cc 0x18298ed90 0x1829c8508 0x18fd95468 0x18fd951c0 0x18fe1b82c 0x18fe1b380 0x1901932c0 0x18fa41114 0x18fe69850 0x18fa1ebe4 0x18fa1e1f4 0x18d2d6e44 0x18d2d7fb4 0x1812070d0 0x181217d90 0x181152098 0x1811578a4 0x18116b468 0x19cd0f38c 0x183b0e5d0 0x18388cf74 0x188e2a314 0x188d59508 0x188d3acb8 0x104f080d4 0x104f0818c 0x10510daa4)
libc++abi: terminating with uncaught exception of type NSException
dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<WKWebView: 0x106025e00; frame = (0 0; 428 796); layer = <CALayer: 0x283bb1b80>>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: URL
Observed object: <WKWebView: 0x106025e00; frame = (0 0; 428 796); layer = <CALayer: 0x283bb1b80>>
Change: {
kind = 1;
new = "https://www.runixcoin.com/dashboard/auth-password-basic.php";
}
Context: 0x0'
terminating with uncaught exception of type NSException
(lldb)