[Swift通天遁地]一、超级工具-(6)通过JavaScript(脚本)代码调用设备的源生程序

本文将演示如何通过JavaScript(脚本)代码调用设备的源生程序。

在项目文件夹【DemoApp】上点击鼠标右键,弹出右键菜单。

【New File】->【Blank】空白模板->【next】

->【Save As】:GetDeviceInfo.html->【Create】

在GetDeviceInfo.html中输入网页代码:

 1 <!DOCTYPE HTML>
 2 <html>
 3     <head>
 4         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5         <title>getDeviceInfo</title>
 6         <script>
 7             function getDeviceInfo()
 8             {
 9                 document.location = "callios:getDeviceInfo"
10             }
11         </script>
12     </head>
13     <body style="background-color:#ff7e00">
14         <input type="button" value="Get device information" onClick="getDeviceInfo()" style="width:305px;height:50px;font-size:20px;"/>
15     </body>
16 </html>

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

现在开始编写代码,通过网页视图加载刚刚创建的网页文件,并监听网页视图的加载动作。

 1 import UIKit
 2 
 3 //添加一个网页视图的代理协议UIWebViewDelegate
 4 //通过该协议中的方法,可以对网页视图的加载动作进行监听
 5 class ViewController: UIViewController, UIWebViewDelegate {
 6     
 7     //添加一个网页视图对象,作为当前类的属性
 8     var webView:UIWebView!
 9     override func viewDidLoad() {
10         super.viewDidLoad()
11         // Do any additional setup after loading the view, typically from a nib.
12         
13         //获得当前设备的屏幕尺寸信息
14         let bounds = UIScreen.main.bounds
15         //通过屏幕尺寸信息创建一个矩形的显示区域
16         let frame = CGRect(x: 0, y: 60, width: bounds.width, height: bounds.height-60)
17         
18         //初始化一个网页视图对象,并以矩形区域作为其显示区域
19         webView = UIWebView(frame: frame)
20         //设置网页视图的代理对象,
21         //该代理对象是当前的视图控制器对象
22         webView.delegate = self
23         //设置网页视图的背景颜色为无色
24         webView.backgroundColor = UIColor.clear
25         
26         //设置根视图的背景颜色为橙色
27         self.view.backgroundColor = UIColor.orange
28         //将网页视图添加到根视图中
29         self.view.addSubview(webView)
30         
31         //获得网页文件在项目中的路径
32         let path = Bundle.main.path(forResource: "GetDeviceInfo", ofType: "html")
33         //并将路径转换成网址的样式
34         let url = URL(string: path!)
35         //通过网页视图的加载请求方法,加载该网址路径下的网页文件
36         webView.loadRequest(NSURLRequest(url: url!) as URLRequest)
37     }
38     
39     //添加一个代理方法,用来监听网页视图的加载动作,
40     //当网页视图即将开始加载动作时,调用此方法。
41     func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
42     {
43         //获得网页视图即将加载的我那个只字符串
44         let url = request.url?.absoluteString
45         //将网址以冒号进行分割,并生成一个包含两个字符串的数组
46         let components = url?.components(separatedBy: ":")
47         //获得数组中的第一个元素
48         let firstElement = components?[0]
49         
50         //如果数组中的第一个元素,和在网页中编写的脚本一致,则执行之后的代码
51         if (components?.count)! > 1 && firstElement! == "callios"
52         {
53             //获得当前设备的模型信息
54             let model = UIDevice.current.model
55             //获得当前设备的操作系统的名称
56             let systemName = UIDevice.current.systemName
57             //获得当前设备的操作系统的版本号
58             let systemVersion = UIDevice.current.systemVersion
59             
60             //将以上获得的信息拼接成一个字符串常量
61             let message = "Device model:"+model+"\\nSystem name:"+systemName+"\\nSystem version:"+systemVersion
62             //调用脚本的警告语句,在网页中打开警告窗口,并显示设备的属性信息
63             webView.stringByEvaluatingJavaScript(from: "alert('" + message + "')")
64             //最后返回false,使3网页视图中止加载的动作。
65             return false
66         }
67         
68         //当网址视图加载的网址,不是我们自定义的网址时,则返回真,以继续网页视图的加载动作。
69         return true
70     }
71 
72     override func didReceiveMemoryWarning() {
73         super.didReceiveMemoryWarning()
74         // Dispose of any resources that can be recreated.
75     }
76 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10142673.html