三种显示网页的方法(swift)
2016-03-02 本文已影响1696人
我真不是他舅

这个页面是一个tableview ,有两个section。第一个section用的两种方法显示网页内容,方法就是内容名啦~;第二个section用的是SFSafariViewController显示网页内容,包含了三个网页。
小demo地址:http://git.oschina.net/TMPercy/WebView
1.跳到safari里显示:
可是看到有back to web view controller,跳到浏览器再打开网页。下面两行是关键代码。
if let url = NSURL(string:"http://www.apple.com/itunes/charts/paid-apps/") {
UIApplication.sharedApplication().openURL(url)
}

2.用UIWebView显示网页内容。
常规的在app中新建一个页面。
关键代码:在子页面中插入一个网页(view did load):
if let url = NSURL(string:"http://www.appcoda.com/contact") {
let request = NSURLRequest(URL: url)
webView.loadRequest(request)
}
再从父页面跳到子页面:
performSegueWithIdentifier("showWebView", sender: self)。
注:ios9后访问http开头而不是“https”的网页报错,
只需要在info.plist里加一个key即可。


3.用sfsafari view controller显示网页
关键代码:
let safariController = SFSafariViewController(URL: url,entersReaderIfAvailable: true)
presentViewController(safariController, animated: true, completion: nil)

三种显示效果如图。看书学的,抽出来记录一下。