JS与iOS之间的通信

移动开发 iOS
[代码]c#/cpp/oc代码:/[代码]js代码:

JS与iOS之间的通信,主要运用两个方法:(PhoneGap框架也是基于此原理)

1、UIWebView的 stringByEvaluatingJavaScriptFromString方法

2、UIWebViewDelegate的

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

[代码]c#/cpp/oc代码:

01 - (void)viewDidLoad
02 {
03 [super viewDidLoad];
04 NSString *path = [[NSBundle mainBundle] pathForResource:@"jm/info" ofType:@"html"];
05 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
06 [self.webView loadRequest:request];
07 }<div><h3>[代码]c#/cpp/oc代码:</h3><pre class="brush:c#/cpp/oc;auto-links:false;">#pragma mark - UIWebViewDelegate
08 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
09 {
10 if([request.mainDocumentURL.relativePath isEqualToString:@"/getInfo/name"])
11 {
12 NSString *info = [[UIDevice currentDevice] name];
13 NSString *js = [NSString stringWithFormat:@"showInfo(\"name\",\"%@\")",info];
14 [self.webView stringByEvaluatingJavaScriptFromString:js];
15 return false;
16 }
17 if([request.mainDocumentURL.relativePath isEqualToString:@"/getInfo/systemVersion"])
18 {
19 NSString *info = [[UIDevice currentDevice] systemVersion];
20 NSString *js = [NSString stringWithFormat:@"showInfo(\"systemVersion\",\"%@\")",info];
21 [self.webView stringByEvaluatingJavaScriptFromString:js];
22 return false;
23 }
24 return true;
25 }</pre></div><br>


 

JS代码:

[代码]js代码:

view source
print?
01 <!DOCTYPE html>
02 <html>
03 <head>
04 <title>city</title>
05 <meta charset="utf-8">
06 <meta name="viewport" content="width=device-width, initial-scale=1">
07 <link rel="stylesheet" href="jquery.mobile-1.0.css"/>
08 <script type="text/javascript" src="jquery.js"></script>
09 <script type="text/javascript" src="jquery.mobile-1.0.js"></script>
10 <script>
11 function getInfo(name)
12 {
13 window.location = "/getInfo/"+name;
14 }
15 function showInfo(id,info)
16 {
17 $("p#"+id).html(info);
18 }
19 </script>
20 </head>
21 <body>
22 <div data-role="page">
23 <div data-role="content">
24 <h2>Divice Info</h2>
25 <div data-role="collapsible-set" data-theme="c" data-content-theme="d">
26 <div data-role="collapsible">
27 <h3 onclick="getInfo('name')">name</h3>
28 <p id="name"></p>
29 </div>
30 <div data-role="collapsible">
31 <h3 onclick="getInfo('systemVersion')">systemVersion</h3>
32 <p id="systemVersion"></p>
33 </div>
34 </div>
35 </div>
36 </div>
37 </body>
38 </html>


 

责任编辑:冰凝儿
相关推荐

2011-03-10 09:07:47

liferayportlet

2009-12-03 18:15:04

Linux

2019-06-25 10:53:06

AndroidFlutter通信

2011-08-31 13:22:37

PhoneGapAndroidjavascript

2012-12-21 09:19:29

Google GO

2010-03-18 19:06:35

Java socket

2021-11-08 07:48:50

管道流通信数据

2009-09-10 11:26:59

C# form

2015-06-30 10:48:41

iOSAPPwarchkit

2018-10-11 05:07:19

物联网通信IOT

2021-12-16 16:20:57

GoWebSocketLinux

2020-07-06 07:52:10

Kubernetes网络通信

2018-08-15 12:43:52

iOSAndroid设计差异

2022-05-30 15:21:27

Hi3861TCP通信

2016-09-14 21:44:50

JavascriptreactJsjsx

2021-02-14 21:05:05

通信消息系统

2021-08-02 08:22:33

BlazorEventCallba通信

2021-08-24 14:57:27

鸿蒙HarmonyOS应用

2013-05-10 13:27:55

FedoraUbuntu

2012-05-31 14:54:59

Hadoop大数据
点赞
收藏

51CTO技术栈公众号