iOS UIWebView Javascript - 插入数据 - 接收回调? [英] iOS UIWebView Javascript - insert data -receive callbacks?
问题描述
我有一个网页。我有一个javascript文件,可以完成很多东西。在应用程序中,我有一个NSString,其中包含一些需要在javascript文件中处理的关键数据。如何将我的字符串值放入我的html中的某个位置,以便我的javascript可以读取它?
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将在执行操作时产生某些数据(如按下按钮),如何从webview / 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方法并使用这个传递值
You can call a javascript method and pass the value by using this
NSString *selectedValue = [webViewInstance stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"getAndReturnRadioValue(%d)", questionCounterIntValue]];
但要从javascript触发,您需要使用变通方法,
But to trigger back from javascript you need to use a workaround method,
实现以下webViewDelegate方法
Implement the following webViewDelegate method
- (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
}
}
您必须从javascript方法导航到假URL,
You have to navigate to a fake URL from your javascript method like,
window.location = "someLink://yourApp/form_Submitted:param1:param2:param3";
。
您可以查看我对此问题的回答
提交表格UIWebView
You could take a look at my answer for this question Submit a form in UIWebView
这篇关于iOS UIWebView Javascript - 插入数据 - 接收回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!