上一篇开始用Swift开发iOS 10 - 20 使用Tab Bar Controller 和 拆分Storyboard学习了工具栏和Storyboard的拆分,这一篇学习怎么在app中显示网页内容。由于原书中使用了的网站在国内不好访问,我就用了我的简书、博客、Github代替🙂。
设计about view
下载图片拖进
Assets.xcasset
。打开
about.storyboard
,拖进一个Image View到table view header,height
为190,image
为about-logo,content mode
为Aspect fit
。选择table view cell,identifier
为Cell
,style
为Basic
。新建
AboutTableViewController
继承至UITableViewController
,关联about中的table view controller。- 在
AboutTableViewController
,添加:1
2
3var sectionTitles = ["Leave Feedback", "Follow Us"]
var sectionContent = [["Rate us on App Store", "Tell us your feedback"], ["Jianshu", "Blog", "Github"]]
var links = ["http://www.jianshu.com/u/efce1a2a95ab", "http://andyron.com", "https://github.com/andyRon"]
table view的相关代理协议方法实现,这次使用俩个section,tableView(_:titleForHeaderInSection:)
是获取section的title方法。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20override func numberOfSections(in tableView: UITableView) -> Int {
return sectionTitles.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return sectionContent[section].count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = sectionContent[indexPath.section][indexPath.row]
return cell
}
移除Tableview下面分割线,在
viewDidLoad
中添加:tableView.tableFooterView = UIView(frame: CGRect.zero)
用Mobile Safari打开Web
可直接给出网址,通过Mobile Safari打开网站,UIApplication.shared.open(_:options:completionHandler:)
。
1 | override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { |
使用WKWebView载入Web
使用使用WKWebView载入Web的例子1
2
3
4if let url = URL(string: "http://andyron.com") {
let request = URLRequest(url: url)
webView.load(request)
}
直接载入本地html文件的例子:1
2
3let url = URL(fileURLWithPath: "about.html")
let request = URLRequest(url: url)
webView.load(request)
- 在
about.storyboard
中拖进一个新的View Controller,用来展示Web内容。使用ctrl-drag建立Show类型的segue,identifier
为showWebView
。
- 新建
WebViewController
继承至UIViewController
,关联上面的用来显示Web内容的View Controller。 - 在
WebViewController.swift
中添加import WebKit
,变量var webView: WKWebView!
。 更新
viewDidLoad
:1
2
3
4
5
6
7
8
9override func viewDidLoad() {
super.viewDidLoad()
if let url = URL(string: "http://andyron.com") {
let request = URLRequest(url: url)
webView.load(request)
}
}添加
loadView
,这个方法会在viewDidLoad
之前调用创建WKWebView
。1
2
3
4override func loadView() {
webView = WKWebView()
view = webView
}更新
AboutTableViewController
中的tableView(_didSelectRowAtIndexPath:)
:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case 0:
if indexPath.row == 0 {
if let url = URL(string: "http://www.apple.com/itunes/charts/paid-apps/") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
} else if indexPath.row == 1 {
performSegue(withIdentifier: "showWebView", sender: self)
}
default:
break
}
// 取消被选中状态
tableView.deselectRow(at: indexPath, animated: false)
}由于自从iOS 9之后,出于安全考虑,默认只能访问HTTPS的网站,如果需要访问HTTP的网站,就需要在
plist
文件中添加许可:
使用SFSafariViewController载入Web
在AboutTableViewController.swift
中加入import SafariServices
,然后更新tableView(_didSelectRowAtIndexPath:)
,只用通过url创建一个SFSafariViewController
对象,然后使用present
方法展示就可以了。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case 0:
if indexPath.row == 0 {
if let url = URL(string: "http://www.apple.com/itunes/charts/paid-apps/") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
} else if indexPath.row == 1 {
performSegue(withIdentifier: "showWebView", sender: self)
}
case 1:
if let url = URL(string: links[indexPath.row]) {
let safariController = SFSafariViewController(url: url)
present(safariController, animated: true, completion: nil)
}
default:
break
}
// 取消被选中状态
tableView.deselectRow(at: indexPath, animated: false)
}
代码
Beginning-iOS-Programming-with-Swift
说明
此文是学习appcode网站出的一本书 《Beginning iOS 10 Programming with Swift》 的一篇记录