简体   繁体   English

如何在iOS中将变量从JavaScript传递到目标C,将目标C传递给Javasctipt

[英]How to pass a variable from javascript to objective C and Objective C to Javasctipt in ios

I am a iOS Developer, am new to javascript. 我是iOS开发人员,是javascript新手。

I wants to create a communication between Javascript to Objective C and Objective C to Javascript. 我想在目标Java的JavaScript与目标Java的JavaScript之间建立通信。

How to pass a variable from javascript to objective C and Objective C to Javasctipt in ios. 如何在ios中将变量从javascript传递给目标C,将目标C传递给Javasctipt。

If any one have references please let us know about this? 如果有参考文献,请告诉我们吗?

I program JavaScript when using Parse CloudCode. 我在使用Parse CloudCode时编写JavaScript。 In my case, I use CloudCode to call the Stripe API. 就我而言,我使用CloudCode调用Stripe API。

For example: 例如:

Objective-C 目标C

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

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

JavaScript 的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. 如您所见,在这种情况下,要将变量从Objective-C传递给javascript,请使用request.params。

You can use native iOS objects to do this. 您可以使用本机iOS对象执行此操作。

From Obj-C to JS 从Obj-C到JS

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

It injects or calls pre-existing method/object in the page loaded in the UIWebview. 它在UIWebview加载的页面中注入或调用预先存在的方法/对象。 It returns the value that JS return to you 它返回JS返回给您的值

UIWebView Class Reference UIWebView类参考

From JS to Obj-C 从JS到Obj-C

Implementing UIWebView protocol you can handle the request before it will start: 实施UIWebView协议,您可以在请求开始之前对其进行处理:

- (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:// . 因此,从JS中,您必须使用自定义模式http(例如test://调用URL。 Then you will parse the request in the delegate method I wrote before. 然后,您将使用我之前编写的委托方法来解析请求。

If you are targeting iOS8, you could consider to use WKWebView rather than UIWebView , which has considerably improved in this regard. 如果您的目标是iOS8,则可以考虑使用WKWebView而不是UIWebView ,它在这方面已经有了很大的改进。 Good starting points are the the WKWebView reference docs and this NSHipster article . WKWebView参考文档NSHipster这篇文章是一个很好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM