How To Make Website And Ios App Communicate
6 Answers 6
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]];
answered Nov 29 '11 at 8:27
Björn KaiserBjörn Kaiser
9,812 4 gold badges 36 silver badges 57 bronze badges
4
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.
answered Dec 27 '12 at 5:49
MuraliMurali
188 1 silver badge 11 bronze badges
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
answered Mar 5 '13 at 3:49
Tim CoulterTim Coulter
414 5 silver badges 11 bronze badges
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:
.
answered Nov 29 '11 at 8:26
Johan KoolJohan Kool
15.2k 8 gold badges 60 silver badges 80 bronze badges
2
-
hi, here is how i implemented it:
Mar 13 '12 at 1: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.
Mar 13 '12 at 1:48
[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/
answered Jul 20 '12 at 15:49
dplusmdplusm
3,528 2 gold badges 12 silver badges 6 bronze badges
answered Jan 3 '14 at 12:17
Not the answer you're looking for? Browse other questions tagged javascript ios or ask your own question.
How To Make Website And Ios App Communicate
Source: https://stackoverflow.com/questions/8307620/communication-between-ioss-native-app-and-webpages-javascript
Posted by: jeromefrovessiom.blogspot.com
thanks, It works. is there a way which allows to call iOS's function from javascript?
Nov 29 '11 at 8:52
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.
Nov 29 '11 at 9:30
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: iphonedevelopertips.com/cocoa/…
Dec 2 '11 at 14:24
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:
.Jan 9 '12 at 11:45