Swift - 使用HTML5进行iOS开发(将HTML5打包成iOS应用)

原来我们一直使用UIWebView来加载web页面。从iOS8起,苹果提供了WKWebView用来代替UIWebView。

references”。如果你的html页面有层次结构,比如css,js,图片都放在各自的子文件夹中。要选择后面那个方式“Create
folder
references”。如果选第一个,虽然在Xcode组织树看来都是好的,但实际所有加入到项目的文件都会在mainBundle根路径下,这样文件
引用就会出问题。)

(1)下面是使用UIWebView的样例:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import UIKitimport WebKitclass ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let path = NSBundle.mainBundle().pathForResource("index", ofType: ".html", inDirectory: "HTML5") let url = NSURL(fileURLWithPath:path!) let request = NSURLRequest(URL:url) //将浏览器视图全屏(在内容区域全屏,不占用顶端时间条) let theWebView:UIWebView = UIWebView(frame:UIScreen.mainScreen().applicationFrame) //let theWebView:WKWebView = WKWebView(frame:UIScreen.mainScreen().applicationFrame) //禁用页面在最顶端时下拉拖动效果 theWebView.scrollView.bounces = false //加载页面 theWebView.loadRequest(request) self.view.addSubview(theWebView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }} |
(2)下面是使用WKWebView的样例:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import UIKitimport WebKitclass ViewController: UIViewController { var theWebView:WKWebView? override func viewDidLoad() { super.viewDidLoad() super.viewDidLoad() let path = NSBundle.mainBundle().pathForResource("index", ofType: "html",inDirectory: "HTML5") let url = NSURL(fileURLWithPath: path!) let request = NSURLRequest(URL: url) theWebView = WKWebView(frame: UIScreen.mainScreen().bounds) theWebView!.loadRequest(request) self.view.addSubview(theWebView!) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }} |
3,swift代码与页面js互相调用(使用WKWebView)
下面通过一个简单样例演示js与原生代码如何进行相互调用以及参数传递。当点击一个商品图片时,会弹出一个iOS的消息框。当用户选择确定后,又会调用页面js方法,把商品添加到购物车里面。

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
import UIKitimport WebKitclass ViewController: UIViewController ,WKScriptMessageHandler { var theWebView:WKWebView? override func viewDidLoad() { super.viewDidLoad() let path = NSBundle.mainBundle().pathForResource("index", ofType: "html",inDirectory: "HTML5") let url = NSURL(fileURLWithPath: path!) let request = NSURLRequest(URL: url) //创建供js调用的接口 let theConfiguration = WKWebViewConfiguration() theConfiguration.userContentController.addScriptMessageHandler(self, name: "interOp") theWebView = WKWebView(frame: self.view.frame, configuration: theConfiguration) theWebView!.loadRequest(request) self.view.addSubview(theWebView!) } //响应处理js那边的调用 func userContentController(userContentController:WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) { let sentData = message.body as! NSDictionary //判断是确认添加购物车操作 if(sentData["method"] as? String == "addToCarCheck"){ //获取商品名称 let itemName = sentData["name"] as! String let alertController = UIAlertController(title: "系统提示", message: "确定把\(itemName)添加到购物车吗?", preferredStyle: UIAlertControllerStyle.Alert) let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil) let okAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: { action in print("点击了确定") //调用页面里加入购物车js方法 self.theWebView!.evaluateJavaScript("addToCar('\(itemName)')", completionHandler: nil) }) alertController.addAction(cancelAction) alertController.addAction(okAction) self.presentViewController(alertController, animated: true, completion: nil) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<script>$(function() { //点击商品添加到购物车 $(".goodsItem").click(function() { var itemName = $(this).children("img")[0].alt; var message = {"method":"addToCarCheck","name":itemName}; window.webkit.messageHandlers.interOp.postMessage(message); });}); //添加到购物车function addToCar(itemName){ //这里只是简单的给数量+1,用来演示 var num = parseInt($("#cartNums").text()); $("#cartNums").text(num+1);} </script> |
源码下载:
HTML5.zip
原文出自:www.hangge.com 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_876.html
Swift - 使用HTML5进行iOS开发(将HTML5打包成iOS应用)的更多相关文章
- 李洪强iOS开发之零基础学习iOS开发】【02-C语言】01-概述
前面已经给大家介绍了iOS开发相关的一些基础知识,其实iOS开发就是开发iPhone\iPad上的软件,而要想开发一款软件,首先要学习程序设计语言.iOS开发需要学习的主要程序设计语言有:C语言.C+ ...
- iOS开发技巧 - Size Class与iOS 8多屏幕适配(一)
0. 背景: 在iOS开发中,Auto Layout(自动布局)能解决大部分的屏幕适配问题. 但是当iPhone 6和iPhone 6 Plus发布以后, Auto Layout已经不能解决复杂的屏幕 ...
- Chrome Apps将可以打包成iOS或Android应用
Chrome Apps 将可以在 iOS 和 Android 设备上独立运行了.开发者只要使用 Google今天 提供的工具集(toolchain)将自己的 Web App 打包,并将生成的应用上传到 ...
- 把H5打包成IOS APP其实可以很简单!签名?越狱?都不需要!
很多小伙伴都在开发自己的app, 有的实现实现比较简单,就是一个h5页面,然后想要打包成app发布出去. 这个想法很单纯 打包生成个app这个是很简单的,网上一堆打包工具,分分钟可以完成 BUT…… ...
- [iOS]如何把App打包成ipa文件,然后App上架流程[利用Application Loader]
摘自:http://www.cnblogs.com/wangqi1221/p/5240281.html 在上一篇博客已经讲过上传项目了,但是有的时候,需要我们进行打包成ipa包到别的手机上跑(但是前提 ...
- Android开发把项目打包成apk
做完一个Android项目之后,如何才能把项目发布到Internet上供别人使用呢?我们需要将自己的程序打包成Android安装包文件--APK(Android Package),其后缀名为" ...
- MFC Activex 开发、ocx打包成cab、部署、测试、自动升级
小小抱怨下:也许是MFC现在用的人少的缘故.在国内和国外都基本上找不到什么全的资料.特别是ocx打包成Cab时的安装文件inf的编写方面,国内基本上是copy,抄的还一知半解.查找个资源真心的累啊.现 ...
- Android开发把项目打包成apk-(转)
做完一个Android项目之后,如何才能把项目发布到Internet上供别人使用呢?我们需要将自己的程序打包成Android安装包文件--APK(Android Package),其后缀名为" ...
- (转)Android开发把项目打包成apk
转:http://blog.csdn.net/luoyin22/article/details/7862742 做完一个Android项目之后,如何才能把项目发布到Internet上供别人使用呢?我们 ...
随机推荐
- Spring配置文件模板
模板: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...
- thinkphp小技巧
if(IS_POST) _404("页面不存在",U("index")) //如果不是POST提交,则跳转到index.debug模 ...
- 使用Jquery后去div个数
<div id="tree1" class="tree-folder-content"> <div class="tree-fold ...
- STL insert()使用
下面我以vector的insert()为例: c++ 98: single element (1) iterator insert (iterator position, const value_ty ...
- Lync 2010升级到Lync 2013POC计划-过程!
最近在协助一家客户做升级项目调研,目前处在POC过程中,根据他们的需求我们将整个POC过程用Project 进行了下整理,了解整个项目中可能存在的风险和相应的计划过程,根据相应的计划我们能够将相应过程 ...
- Ultra-QuickSort(归并排序+离散化树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50517 Accepted: 18534 ...
- C++_01_入门
一.类的定义 Person.h类声明 Person.cpp类实现 main.cpp主函数 二.命名空间的使用 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv ...
- 数据库 isnull()、nvl()、ifnull() 使用
SqlServer 插入字段值 为空 设置默认值 isnull(val_1,val_2) 当val_1为 null 的时候 返回 val_2的值 Oracle nvl(val_1,val_2) 当va ...
- iOS转场动画
文顶顶 最怕你一生碌碌无为 还安慰自己平凡可贵 iOS开发UI篇—核心动画(转场动画和组动画) iOS开发UI篇—核心动画(转场动画和组动画) 一.转场动画简单介绍 CAAnimation的子类,用于 ...
- C#_会员管理系统:开发三(修改密码)
为以后多个功能界面考虑,新增一个主界面: 主界面如下: 主界面(VIPMain.cs)详细代码如下: using System; using System.Collections.Generic; u ...