2

I am supposed to communicate between Objective C and javascript code, i also want to return objects from objective c to js and js back to objective c. For that I used a library called Webviewjavascriptbridge. But i could not get a return value from objective c to js. Can some one tell me if there is any other library that can achieve this? I tried with the libraries referred in this link iOS JavaScript bridge but i could not get a return value from objective c to js.

Community
  • 1
  • 1
user1878200
  • 87
  • 1
  • 3
  • In iOS7, JavascriptCore.Framework is supported. Recommend this library, you can communicate between javascript and objc easily with UIWebView. https://github.com/liaojinxing/HybridBridge – liaojinxing Mar 26 '14 at 02:59

3 Answers3

3

JSBridge is an easy to use library to communicate between JS and cocoa/Objective-c`.

http://code.google.com/p/jsbridge-to-cocoa/

If you want to send a message to Javascript from app then UIWebView has a method for you. This is a code I implemented to assign filled data in webview to a Customer.

NSString *jsSave=[NSString stringWithFormat:@"set_customer(%@);",self.customer.customerId];
[webView stringByEvaluatingJavaScriptFromString:jsSave]; 

set_customer is the method of JS.

Abid Hussain
  • 1,529
  • 1
  • 15
  • 33
  • Hi Abid Thanks for your answer, is it possible to return objects from objective c to js by using this library. – user1878200 Dec 07 '12 at 14:23
  • Yes you can. if you visit above mentioned link then you'll see step-by-step instruction of how to use this library. They also have a sample xCode project for help. – Abid Hussain Dec 07 '12 at 14:35
2

Check this out, might be what you need:

http://blog.techno-barje.fr/post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript/

Ismael
  • 3,927
  • 3
  • 15
  • 23
  • Hi ismael, thanks for your answer. i have checked the link but that doesnt provide any way to pass objects from js to obj c.if i am wrong,please let me know how to achieve it. – user1878200 Dec 07 '12 at 14:26
  • Well you can't pass an object directly, you need to make a json and work with that – Ismael Dec 07 '12 at 18:29
  • Absolutely awesome and simple idea, the one in the link, I must say. Copying here the most important fact: `The only thing we have to change is in Javascript code. Instead of changing the document location, we create an IFrame and set its location to a value that trigger the shouldStartLoadWithRequest method.` – cprcrack Nov 27 '13 at 13:18
0

You want a return value from ObjC to js. Maybe that means your js code needs a parameter provided by ObjC code. Then you can try this:

NSString *returnValue = [self someMethod];
NSString *jsCallBack = [NSString stringWithFormat:@"yourJsMethodName:('%@')", returnValue];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];

Is that what you want? I hope it helps.

Gon
  • 1,135
  • 12
  • 22