01. //
02. // ViewController.m
03. // IOS_JS_01
04. //
05. // Created by IMAC on 14-2-25.
06. // Copyright (c) 2014年 Wanggsx. All rights reserved.
07. //
08.
09. # import "ViewController.h"
10.
11. @interface ViewController ()
12.
13. @end
14.
15. @implementation ViewController
16. @synthesize webview;
17.
18. - ( void )viewDidLoad
19. {
20. [ super viewDidLoad];
21. //设置webView
22. webview.backgroundColor = [UIColor clearColor];
23. //webview.scalesPageToFit =YES;
24. webview.delegate =self;
25. //找到jsIOS.html文件的路径
26. NSString *basePath = [[NSBundle mainBundle]bundlePath];
27. NSString *helpHtmlPath = [basePath stringByAppendingPathComponent:@ "jsIOS.html" ];
28. NSURL *url = [NSURL fileURLWithPath:helpHtmlPath];
29. //加载本地html文件
30. [webview loadRequest:[NSURLRequest requestWithURL:url]];
31. }
32.
33. /*
34. * 点击事件
35. * 调用javaScript的方法postStr(str1,str2)并取得返回值
36. * 输出返回值到控制台
37. */
38. -(IBAction)IOS_JS:(id)sender
39. {
40. //NSString *str = [self.webview stringByEvaluatingJavaScriptFromString:@"postStr();"];
41. //要传递的参数一
42. NSString *str1 = @ "我来自于oc" ;
43. //要传递的参数二
44. NSString *str2 = @ "我来自于地球" ;
45. NSString *str = [self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@ "postStr('%@','%@');" ,str1,str2]];
46. NSLog(@ "JS返回值:%@" ,str);
47. }
48.
49.
50. - ( void )didReceiveMemoryWarning
51. {
52. [ super didReceiveMemoryWarning];
53. }
54. @end
|