0

I am a iOS Developer, am new to javascript.

I wants to create a communication between Javascript to Objective C and Objective C to Javascript.

How to pass a variable from javascript to objective C and Objective C to Javasctipt in ios.

If any one have references please let us know about this?

3 Answers3

0

I program JavaScript when using Parse CloudCode. In my case, I use CloudCode to call the Stripe API.

For example:

Objective-C

NSDictionary *parameters = @{
                                 @"customerId": stripeCustomerId,
                                 @"amount": @(100),      
                            };

[PFCloud callFunctionInBackground:@"chargeCustomer" withParameters:parameters block:block];

JavaScript

Parse.Cloud.define("chargeCustomer", function(request, response) {
  Stripe.Charges.create({
      amount: request.params.amount, // in cents
      currency: "usd",
      customer: request.params.customerId,
    },{
    success: function (httpResponse) {
        console.log(httpResponse);
        response.success(httpResponse);
    },
    error: function (httpResponse) {
        console.error(httpResponse.message);
        response.error(httpResponse.message);
    }
  });
});

As you can see, to pass on the variable from objective-c to javascript in this case, you use request.params.

Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
  • This answer is very specific to Parse, while the OP asked for a general approach to communicate between native and JS code. – Daniel Rinser Jan 23 '15 at 08:30
0

You can use native iOS objects to do this.

From Obj-C to JS

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script

It injects or calls pre-existing method/object in the page loaded in the UIWebview. It returns the value that JS return to you

UIWebView Class Reference

From JS to Obj-C

Implementing UIWebView protocol you can handle the request before it will start:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest (NSURLRequest *)request navigationType (UIWebViewNavigationType)navigationType

So, from JS you have to call a URL with a custom schema http such as test://. Then you will parse the request in the delegate method I wrote before.

Phillip Senn
  • 46,771
  • 90
  • 257
  • 373
Luca D'Alberti
  • 4,749
  • 3
  • 25
  • 45
  • If u have any idea about JS to Obj-C, can u explain with an example.., Actually I have tried.. but not achieve.. am unable to get the objects from js to ObjC – Josh Kethepalli Jan 23 '15 at 08:39
  • From JS call `window.location = "test://functionName?queryString";` and in the method listed before, check for the schema: `if ([request URL] scheme] isEqualToString: @"test"]{ // doSomething }` – Luca D'Alberti Jan 23 '15 at 08:46
  • May this will help you? http://stackoverflow.com/questions/9473582/ios-javascript-bridge – Luca D'Alberti Jan 23 '15 at 08:47
0

If you are targeting iOS8, you could consider to use WKWebView rather than UIWebView, which has considerably improved in this regard. Good starting points are the the WKWebView reference docs and this NSHipster article.

Daniel Rinser
  • 8,855
  • 4
  • 41
  • 39