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上供别人使用呢?我们 ...
随机推荐
- 「OC」构造方法和分类
一.构造方法 (一)构造方法的调用 创建一个可用的对象:Person *p=[Person new]; new方法实际上是分为两步来创建一个对象: 1)使用+alloc方法来分配存储空间(返回分配的对 ...
- The Priest Mathematician
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=31329#problem/F f[0] = 1 , f[ i ] = f[ i - 1 ] ...
- gcc/g++ 如何支持c11 / c++11标准编译
如果用命令 g++ -g -Wall main.cpp 编译以下代码 : /* file : main.cpp */ #include <stdio.h> int main() { in ...
- hdu 1236 1.3.2排名
排名 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...
- 使用SharedPreferences即时存储之后,不能即时获取到数据
在这里简介一下我所遇到的情况,由于情况非常特殊,所以我就来记录一下自己在这个方面的经历! 事由:在我所做的app中有一个视频的播放功能,因为之前做优化的时候.我听说对于视频这种比較耗费资源的应该给他独 ...
- sharepoint 2013 附件控件FileUpload怎样检验是否为图片的方法
记录一下关于附件控件FileUpload怎样检验是否为图片的方法: function checkImg() { var fileObj =document.getElementById('<%= ...
- Android Texting(2)Testing Fundamentals 测试基础篇
Testing Fundamentals The Android testing framework, an integral part of the development environment, ...
- MSSQLServer的备份与还原
最近用到了mssql2000的数据备份还原到2008上, 在备份2000时,一定注意要备份成一个文件,就是目标那里只添加一个就好,(否则待会还原数据库时要添加这两个文件,要不就报“备份了几个簇,只提供 ...
- ThinkPHP - CURD增删改查操作
public function show(){ //使用model模型 //1.可以使用 $user = new Model('User'); //2.可以使用 $user = M('User'); ...
- 使用Sphinx生成静态网页
转载来自 http://www.ibm.com/developerworks/cn/opensource/os-sphinx-documentation/ 简介 Sphinx 是一种工具,它允许开发人 ...