★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-qthzbijc-ex.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
目录:[Swift]通天遁地Swiftios
本文将演示如何经过JavaScript(脚本)代码调用设备的源生程序。git
在项目文件夹【DemoApp】上点击鼠标右键,弹出右键菜单。github
【New File】->【Blank】空白模板->【next】web
->【Save As】:GetDeviceInfo.html->【Create】swift
在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】微信
如今开始编写代码,经过网页视图加载刚刚建立的网页文件,并监听网页视图的加载动做。ide
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 }