WKWebview - Complex communication between Javascript & native code
Unfortunately I couldn't find a native solution.
But the following workaround solved my problem
Use javascript promises & you can call the resolve function from your iOS code.
UPDATE
This is how you can use promise
In JS
this.id = 1;
this.handlers = {};
window.onMessageReceive = (handle, error, data) => {
if (error){
this.handlers[handle].resolve(data);
}else{
this.handlers[handle].reject(data);
}
delete this.handlers[handle];
};
}
sendMessage(data) {
return new Promise((resolve, reject) => {
const handle = 'm'+ this.id++;
this.handlers[handle] = { resolve, reject};
window.webkit.messageHandlers.<yourHandler>.postMessage({data: data, id: handle});
});
}
in iOS
Call the window.onMessageReceive
function with appropriate handler id
Call JavaScript function from native code in WKWebView
(I filed a Radar for this shortly after asking the question here.)
A new method was just added a few days ago (thanks jcesarmobile for pointing it out):
Add
-[WKWebView evaluateJavaScript:completionHandler:]
http://trac.webkit.org/changeset/169765
The method is available in iOS 8 beta 3 and up. Here's the new method signature:
/* @abstract Evaluates the given JavaScript string.
@param javaScriptString The JavaScript string to evaluate.
@param completionHandler A block to invoke when script evaluation completes
or fails.
@discussion The completionHandler is passed the result of the script evaluation
or an error.
*/
- (void)evaluateJavaScript:(NSString *)javaScriptString
completionHandler:(void (^)(id, NSError *))completionHandler;
Docs are available here: https://developer.apple.com/documentation/webkit/wkwebview/1415017-evaluatejavascript.
Javascript call to Swift from UIWebView
You must a custom URL Scheme
such as myawesomeapp
and intercept requests to it using:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool
Fire a call to native code using window.location=myawesomeapp://hello=world
, and get the query params you pass from request.URL.query
in the native code.
For more information, see my question about UIWebView
s here: JavaScript synchronous native communication to WKWebView
Related Topics
Swift 2.0: 'Enumerate' Is Unavailable: Call the 'Enumerate()' Method on the Sequence
How to Get a Real Ip Address from Dns Query in Swift
How to Convert a Base64String to String in Swift
Uibezierpath: How to Add a Border Around a View With Rounded Corners
Segue from Modal View to Tab Bar View Controller and Not Lose Tab Bar
Swift 3.0: Convert Server Utc Time to Local Time and Vice-Versa
Any Reason Not Use Use a Singleton "Variable" in Swift
"Classname Has No Member Functionname" When Adding Uibutton Target
How to Create a Reference Cycle Using Dispatchqueues
Difference Between Functions and Closures
How to Avoid Force Unwrapping a Variable
Multi-Touch Gesture in Sprite Kit
Nehotspothelper.Register Not Received Call Back Ios11
Wait Until Swift For Loop With Asynchronous Network Requests Finishes Executing
Main Thread Checker: Ui API Called on a Background Thread: -[Uiapplication Applicationstate]