iOS Swift WebView

WebView

We have a situation to use WebView to show web site contents in your application.
iOS prepares some ways to show web page.
Apple prohibited to show some web pages, but web based game, service faq etc… there are a lot of cases we want to use webview in iOS apps.

Type

  • UIWebView
  • WKWebView(iOS8)
  • SFSafariViewController(iOS9)

In SFSafariViewController Documentation of Apple Developer says,
if you want to customize, please use WKWebView, if you don’t need customize, SFSafariViewController might be better.
So, for simple webview, for example, FAQ, just link, SFSafariViewController is easy, but cannot change UI

The difference is xxView, UIView base, but SFSafariViewController is UIViewController base, modal like.
I heard UIWebView in iOS9 is a bit unstable.

Sample Program

This is a sample to use
WebViews

To use WKWebView, you need to add WebKit.framework
To use SFSafariViewController, you need to add SafariServices.framework

ViewController.swift

import UIKit
import SafariServices

class ViewController: UIViewController {

    var safariButton : UIButton?
    
    var wkButton : UIButton?
    
    var uiButton : UIButton?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.whiteColor()
        
        self.uiButton = createButton("UIWebView", action: "uiClick", color: UIColor.orangeColor())
        self.wkButton = createButton("WKWebView", action: "wkClick", color: UIColor.blueColor())
        self.safariButton = createButton("Safari", action: "safariClick", color: UIColor.blackColor())
        
        self.view.addSubview(self.uiButton!)
        self.view.addSubview(self.wkButton!)
        self.view.addSubview(self.safariButton!)
    }

    func createButton(title : String, action: Selector, color: UIColor) -> UIButton {
        let button : UIButton = UIButton(type: UIButtonType.RoundedRect);
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.backgroundColor = color
        button.addTarget(self, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func viewWillLayoutSubviews() {
        
        let frame : CGRect = self.view.frame
        self.safariButton?.frame = CGRectMake(20, 100, frame.width - 40, 40)
        self.wkButton?.frame = CGRectMake(20, 150, frame.width - 40, 40)
        self.uiButton?.frame = CGRectMake(20, 200, frame.width - 40, 40)
    }
    
    func uiClick() {
        let uwc = UIWebViewController()
        self.presentViewController(uwc, animated: true, completion: nil)
    }
    
    func wkClick() {
        let wvc = WKViewController()
        self.presentViewController(wvc, animated: true, completion: nil)
    }
    
    func safariClick(sender : AnyObject) {
        // Read only address bar
        let svc = SFSafariViewController(URL: NSURL(string: "http://www.google.com")!, entersReaderIfAvailable: true)
        self.presentViewController(svc, animated: true, completion: nil)
    }
}

WKViewController.swift

import UIKit
import WebKit

class WKViewController: UIViewController {
    
    var webView : WKWebView?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.webView = WKWebView()
        self.view.addSubview(self.webView!)
        self.webView?.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.google.com")!))
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillLayoutSubviews() {
        let frame : CGRect = self.view.frame
        self.webView?.frame = frame
    }
}

UIWebViewController.swift

import UIKit

class UIWebViewController : UIViewController {
    
    var webView : UIWebView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.webView = UIWebView()
        self.view.addSubview(self.webView!)
        self.webView?.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.google.com")!))
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillLayoutSubviews() {
        let frame : CGRect = self.view.frame
        self.webView?.frame = frame
    }
}