iOS UIWebView Javascript - 插入数据 - 接收回调?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7681918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:55:42  来源:igfitidea点击:

iOS UIWebView Javascript - insert data -receive callbacks?

javascriptiphoneios

提问by spentak

I have a webpage. I have a javascript file that does a whole lot of stuff. In the app I have an NSString with some crucial data that needs to be processed in the javascript file. How do I get my string value into a certain place in my html so my javascript can read it?

我有一个网页。我有一个 javascript 文件,可以做很多事情。在应用程序中,我有一个 NSString,其中包含一些需要在 javascript 文件中处理的关键数据。如何将我的字符串值放入我的 html 中的某个位置,以便我的 javascript 可以读取它?

Also, the javascript will produce certain data when actions are performed (like pressing a button), how do I retrieve the data from the webview/javascript so it can be stored in the app?

此外,javascript 会在执行操作(例如按下按钮)时生成某些数据,如何从 webview/javascript 检索数据以便将其存储在应用程序中?

回答by sElanthiraiyan

You can call a javascript method and pass the value by using this

您可以调用 javascript 方法并使用此方法传递值

NSString *selectedValue = [webViewInstance stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"getAndReturnRadioValue(%d)", questionCounterIntValue]];

But to trigger back from javascript you need to use a workaround method,

但是要从 javascript 触发回来,您需要使用一种解决方法,

Implement the following webViewDelegate method

实现以下 webViewDelegate 方法

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

    NSLog(@"passed data from web view : %@",[[request URL] query]);

    if([[[request URL] query] isEqualToString:@"clickedOnLineNo"])
    {
        //Do your action here    
    }
}

You have to navigate to a fake URL from your javascript method like,

你必须从你的 javascript 方法导航到一个假的 URL,比如,

window.location = "someLink://yourApp/form_Submitted:param1:param2:param3";

on a button click or your required action.

单击按钮或您需要的操作。

You could take a look at my answer for this question Submit a form in UIWebView

你可以看看我对这个问题的回答 Submit a form in UIWebView

回答by Matthew Gillingham

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

sounds like exactly the method you need. If, for example, the input and output need to interact with the JavaScript can be done with a single method, you can pass a value into JavaScript and get the results with something like the following code:

听起来正是您需要的方法。例如,如果输入和输出需要与 JavaScript 交互可以通过单个方法完成,您可以将一个值传递给 JavaScript 并使用以下代码获得结果:

NSString* jsCode = [NSString stringWithFormat:@"doSomething('%@')", dataToPass];
NSString* resultFromJavaScript = [webView stringByEvaluatingJavaScriptFromString:jsCode];

回答by user3540599

Hi we can send the data from objective c to javascript and can callback from javascript easily

嗨,我们可以将数据从目标 c 发送到 javascript,并且可以轻松地从 javascript 回调

Objective-C --> javaScript

Objective-C --> JavaScript

In Objective-C class

在 Objective-C 类中

NSData *locationCountData = [NSJSONSerialization dataWithJSONObject:locationCount   options:NSJSONWritingPrettyPrinted error:nil];
NSString *locationCountString = [[NSString alloc] initWithData:locationCountData encoding:NSUTF8StringEncoding];

NSData *locationCountData = [NSJSONSerialization dataWithJSONObject:locationCount options:NSJSONWritingPrettyPrinted error:nil];
NSString *locationCountString = [[NSString alloc] initWithData:locationCountData encoding:NSUTF8StringEncoding];

NSString *str = [NSString   stringWithFormat:@"testFunction(%@,%@)",locationCountString, locationsListString];
[self.myWebView stringByEvaluatingJavaScriptFromString: str];

in JavaScript file

在 JavaScript 文件中

<script type="text/javascript">
function testFunction(locationCount,locationList)
{
    alert(locationCount + locationList);
}
</Script>