7

I have a webpage loaded in a UIWebView, and a javascript function of the page needs to data from native iOs app, a NSString. How can a Js function access the data in native app?

Thanks,

lvreiny

jAckOdE
  • 2,402
  • 8
  • 37
  • 67

6 Answers6

11

You can execute JavaScript in your UIWebView from Obj-C. Simply call [webView stringByEvaluatingJavaScriptFromString:@"myJavaScript"];.

I could imagine a setup like this:

Webpage

<html>
   <head>
      <script type="text/javascript">
         function callmeFromObjC(para1) {
             // do something
             alert(para1);
         }
      </script>
   </head>
   <body>
   </body>
</html>

Objective-C

NSString *myParameter = @"myParameter";
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"callmeFromObjC('%@')", myParameter]];
Björn Kaiser
  • 9,882
  • 4
  • 37
  • 57
  • thanks, It works. is there a way which allows to call iOS's function from javascript? – jAckOdE Nov 29 '11 at 08:52
  • 1
    Did you even read my answer? I know it doesn't have sample code, but it has the answer to the question in your comment. – Johan Kool Nov 29 '11 at 09:30
  • 3
    Implement a custom URL scheme like Johan Kool suggested and then call your url like this from JS "myapp://parameter1/parameter2/parameter3" to make this work you also need to implement the method "- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url" in your app delegate. Here's a complete tutorial about just that: http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html – Björn Kaiser Dec 02 '11 at 14:24
  • 3
    Björn is correct, except that you don't necessarily need to overwrite `-application:handleOpenURL:`. Usually it's easier to do this in `-webView:shouldStartLoadWithRequest:navigationType:`. – Johan Kool Jan 09 '12 at 11:45
7

With WebViewJavaScriptBridge you can achieve two way communication between javaScript and iOS.

Check this link below for WebViewJavaScriptBridge .

I used this bridge for one of my application for communication between iOS and JS and also vice versa.

https://github.com/marcuswestin/WebViewJavascriptBridge.

Murali
  • 188
  • 1
  • 11
4

I created an iOS/JS library to help make this easier -- that is, communication in both directions using similar methods. You can check it out here: https://github.com/tcoulter/jockeyjs

Tim Coulter
  • 414
  • 5
  • 11
1
[webView loadHTMLString:@"<script src=\"filename.js\"></script>" 
      baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];

NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"function(parameter)"];

Give feedback to iOS

window.location = customprefix://function/parameter=value

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([[URL scheme] isEqualToString:@"customprefix"]) {
        // handle function name and paramters
    }
}

I also wrote a guide on how to call and handle different javascript functions from within iOS. http://www.dplusmpage.com/2012/07/20/execute-javascript-on-ios/

dplusm
  • 3,548
  • 2
  • 14
  • 6
1

Let the javascript load a custom URL, which your app intercepts. It than can parse it, prepare the data and pass it on to your webpage via stringByEvaluatingJavaScriptFromString:.

Johan Kool
  • 15,637
  • 8
  • 64
  • 81
  • hi, here is how i implemented it: – jAckOdE Mar 13 '12 at 01:36
  • i have a function jsfunc(){ location.href =" a custom url" }, I use webView:shouldStartLoadWithRequest:navigationType to handle the url and call a native function.The problem is that the native function returns a string,and jsfunc() should return that string. How to do it? I made a workaround by calling another js function from native code using stringByEvaluatingJavaScriptFromString and pass the returnstring to it, but it would be better if jsfunc() can return it directly. – jAckOdE Mar 13 '12 at 01:48
0

Sample code for this is available here,you can check it....very usefull

http://ramkulkarni.com/blog/framework-for-interacting-with-embedded-webview-in-ios-application/

Pravin
  • 61
  • 6