evaluateJavaScript WKWebView iOS 15 not working
Asked Answered
O

1

6

I'm using WebCrypto library to decrypt the data but it's not working on iOS 15 Its working fine in iOS 14

I have checked the evaluateJavaScript function of webView is throwing error

Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=27, 
WKJavaScriptExceptionMessage=TypeError: undefined is not an object (evaluating 'y.importKey'), WKJavaScriptExceptionColumnNumber=8260, 
WKJavaScriptExceptionSourceURL=undefined, NSLocalizedDescription=A JavaScript exception occurred}
Obduliaobdurate answered 4/9, 2021 at 12:10 Comment(0)
S
1

I stumbled also upon this, the new IOS15 behavior did break our code too. Apparently Apple changed the semantics of evaluateJavaScript : depending on when the following code has been called the new function does not appear in the JS namespace if it is called too early.

[_webview evaluateJavaScript:@"function foobar() {console.log('in foobar');}" completionHandler:^(NSObject* res,NSError* err) {
}];

if this code is called prior to

[_webview loadRequest:req];

then it doesn't have any effect anymore (But no error is raised at that point..the function lands in nirvana ). The solution is to add such code as user script as pointed out at https://developer.apple.com/forums/thread/684020

WKUserScript *script = [[WKUserScript alloc] initWithSource:@"function foobar() {console.log('in foobar');" injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[_webview.configuration.userContentController addUserScript:script];
Salgado answered 1/10, 2021 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.