[英]iPhone: call and send parameters to JS function from OBJ-C
Any idea how to call and send parameters to JS function from OBJ-C ?? 任何想法如何从OBJ-C调用并将参数发送到JS函数?
Lets say in my html I have JS 可以说在我的html中我有JS
function showPieChart (parameter1, parameter2) { ... }
函数showPieChart(parameter1,parameter2){...}
and from my obj c class in viewDidLoad I want to call this function. 从我在viewDidLoad中的obj c类,我想调用此函数。 Any idea or examples ?
有什么想法或例子吗? Thanks in advance...
提前致谢...
Add this in the webViewDidFinishLoad: of the UIWebViewDelegate: 将此添加到UIWebViewDelegate的webViewDidFinishLoad:中:
NSString *functionCall = [NSString stringWithFormat:@"showPieChart(%@,%@)", param1, param2];
NSString *result = [webView stringByEvaluatingJavaScriptFromString:functionCall];
To set a view controller as delegate of a UIWebView, you have to add the <UIWebViewDelegate>
protocol to the @interface
header and link the delegate field of the UIWebView to the view controller. 要将视图控制器设置为UIWebView的委托,您必须将
<UIWebViewDelegate>
协议添加到@interface
标头,并将UIWebView的委托字段链接到视图控制器。
If your parameters are inside an array, you can extract them one by one with [array objectForIndex:i]
(i being the index for each parameter) or all at once with componentsJoinedByString:
如果您的参数在数组内部,则可以使用
[array objectForIndex:i]
(i是每个参数的索引)一个接一个地提取它们,也可以使用componentsJoinedByString:
一次全部提取它们componentsJoinedByString:
NSArray *params = [NSArray arrayWithObjects:@"a",@"b",nil];
NSString *functionCall = [NSString stringWithFormat:@"showPieChart(%@)", [params componentsJoinedByString:@","]];
1) Implement the UIWebViewDelegate 1)实现UIWebViewDelegate
2) Override the 2)覆盖
-(BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType method.. -(BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)请求navigationType:(UIWebViewNavigationType)navigationType方法..
3) In this method you compare the request with a protocole defined. 3)在这种方法中,您将请求与定义的协议进行比较。
Example: 例:
NSString *requestString = [[request URL] absoluteString]; NSString * requestString = [[请求URL] absoluteString]; NSArray *components = [requestString componentsSeparatedByString:@":"];
NSArray * components = [requestString componentsSeparatedByString:@“:”];
if ([components count] > 1 &&
[(NSString *)[components objectAtIndex:0] isEqualToString:@"myapp"]) {
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"myfunction"])
{
NSLog([components objectAtIndex:2]); // param1
NSLog([components objectAtIndex:3]); // param2
// Call your method in Objective-C method using the above...
}
return NO;
}
4) modify your javascript file to create the protocole: 4)修改您的JavaScript文件以创建协议:
function showPieChart(parameter1,parameter2) { window.location="myapp:" + "myfunction:" + param1 + ":" + param2; 函数showPieChart(parameter1,parameter2){window.location =“ myapp:” +“ myfunction:” + param1 +“:” + param2;
} }
resouce :http://www.codingventures.com/2008/12/using-uiwebview-to-render-svg-files/ 资源:http://www.codingventures.com/2008/12/using-uiwebview-to-render-svg-files/
I'm guessing you have a UIWebView with a web page loaded. 我猜你有一个加载了网页的UIWebView。 To execute JavaScript, do this:
要执行JavaScript,请执行以下操作:
NSString *result = [_webView stringByEvaluatingJavaScriptFromString:@"showPieChart()"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.