UIWebView和WebKit

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)
      
    }
    
  }
  
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值