iOS JavaScript bridge
There are a few libraries, but I didn't used any of these in big projects, so you might want to try them out:
- WebViewJavascriptBridge: https://github.com/marcuswestin/WebViewJavascriptBridge
- GAJavaScript: https://github.com/newyankeecodeshop/GAJavaScript
—
However, I think it's something simple enough that you might give it a try yourself. I personally did exactly this when I needed to do that. You might also create a simple library that suits your needs.
1. Execute JS methods from Objective-C
This is really just one line of code.
NSString *returnvalue = [webView stringByEvaluatingJavaScriptFromString:@"your javascript code string here"];
More details on the official UIWebView Documentation.
2. Execute Objective-C methods from JS
This is unfortunately slightly more complex, because there isn't the same windowScriptObject property (and class) that exists on Mac OSX allowing complete communication between the two.
However, you can easily call from javascript custom-made URLs, like:
window.location = yourscheme://callfunction/parameter1/parameter2?parameter3=value
And intercept it from Objective-C with this:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:@"yourscheme"]) {
// parse the rest of the URL object and execute functions
}
}
This is not as clean as it should be (or by using windowScriptObject) but it works.
3. Listen to native JS events from Objective-C (for example DOM ready event)
From the above explanation, you see that if you want to do that, you have to create some JavaScript code, attach it to the event you want to monitor and call the correct window.location
call to be then intercepted.
Again, not clean as it should be, but it works.
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
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
Related Topics
Coordinates of Selected Text in Browser Page
How to Update Placeholder Color Using JavaScript
Convert Binary Data to Base64 With JavaScript
Prevent Body from Scrolling When a Modal Is Opened
How to Build Simple Jquery Image Slider With Sliding or Opacity Effect
How to Resize a Google Map With JavaScript After It Has Loaded
How to Access and Process Nested Objects, Arrays, or Json
"Variable" Variables in JavaScript
Execute Function After Complete Page Load
Benefits of Loading Js At the Bottom as Opposed to the Top of the Document
Fetch(), How to Make a Non-Cached Request
Classname Only Changing Every Other Class
How to Select Nth Line of Text (Css/Js)
Select All Div Text With Single Mouse Click
Is There a Callback on Completion of a Css3 Animation
How to Change a Class CSS With a Greasemonkey/Tampermonkey Script