loadHTMLString
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let webView = UIWebView(frame: view.bounds)
let htmlString = "<br/>iOS <strong>Programming</strong>"
webView.loadHTMLString(htmlString, baseURL: nil)
view.addSubview(webView)
}
}
效果如下:
loadRequest
import UIKit
class ViewController: UIViewController {
/* Hide the status bar to give all the screen real estate */
override func prefersStatusBarHidden() -> Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
let webView = UIWebView(frame: view.bounds)
webView.scalesPageToFit = true
view.addSubview(webView)
let url = NSURL(string: "http://www.apple.com")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)
view.addSubview(webView)
}
}
效果如下:
如果想做一个简单的新闻客户端,爬到链接后即可通过webview显示新闻内容。
加载网页需要一定的时间,比较好的方式就是像下面所示的图,显示一个网络正在运转的状态
为了实现这个功能,需要借助webview的代理。
代理需要实现三个方法分别是开始加载时、加载结束后、加载失败后的动作。借助
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
来开启、关闭网络旋转的图标。
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
func webViewDidStartLoad(webView: UIWebView){
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
func webViewDidFinishLoad(webView: UIWebView){
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError){
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
override func viewDidLoad() {
super.viewDidLoad()
var frame = view.bounds
frame.origin.y = UIApplication.sharedApplication().statusBarFrame.height
frame.size.height -= frame.origin.y //将webview显示在状态栏下面
let webView = UIWebView(frame: frame)
webView.delegate = self
webView.scalesPageToFit = true
view.addSubview(webView)
let url = NSURL(string: "http://www.apple.com")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)
view.addSubview(webView)
}
}
效果如下:
WebKit
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView?
/* Start the network activity indicator when the web view is loading */
func webView(webView: WKWebView,
didStartProvisionalNavigation navigation: WKNavigation){
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
/* Stop the network activity indicator when the loading finishes */
func webView(webView: WKWebView,
didFinishNavigation navigation: WKNavigation){
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
func webView(webView: WKWebView,
decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse,
decisionHandler: ((WKNavigationResponsePolicy) -> Void)){
print(navigationResponse.response.MIMEType)
decisionHandler(.Allow)
}
override func viewDidLoad() {
super.viewDidLoad()
/* Create our preferences on how the web page should be loaded */
let preferences = WKPreferences()
preferences.javaScriptEnabled = false
/* Create a configuration for our preferences */
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
/* Now instantiate the web view */
webView = WKWebView(frame: view.bounds, configuration: configuration)
if let theWebView = webView{
/* Load a web page into our web view */
let url = NSURL(string: "http://www.apple.com")
let urlRequest = NSURLRequest(URL: url!)
theWebView.loadRequest(urlRequest)
theWebView.navigationDelegate = self
view.addSubview(theWebView)
}
}
}