Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  139] [ 3]  / answers: 1 / hits: 114123  / 13 Years ago, mon, february 27, 2012, 12:00:00

I'm working on an app where I'm going to use both HTML5 in UIWebView and native iOS framework together. I know that I can implement communication between JavaScript and Objective-C. Are there any libraries that simplify implementing this communication? I know that there are several libraries to create native iOS apps in HTML5 and javascript (for example AppMobi, PhoneGap), but I'm not sure if there is a library to help create native iOS apps with heavy JavaScript usage. I need to:




  1. Execute JS methods from Objective-C

  2. Execute Objective-C methods from JS

  3. Listen to native JS events from Objective-C (for example DOM ready event)


More From » ios

 Answers
8

There are a few libraries, but I didn't used any of these in big projects, so you might want to try them out:







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.


[#87175] Sunday, February 26, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monetm

Total Points: 615
Total Questions: 103
Total Answers: 119

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
monetm questions
Fri, Feb 26, 21, 00:00, 3 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Sun, Jul 26, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;