UI控件

 

浏览器控件

在app中显示网页内容几乎是必不可少的。

iOS9 SDK提供3种方式显示网页:

  1. Safair-可以让Safair来打开你指定的URL,app会暂时切换到Safair
  2. UIWebView/WKWebView-前者是iOS8前最常用的浏览器控件,后者是增强版,可把这两者视作精简版的Safair;用来打开一个指定的网页,这2者再合适不过。
  3. SFSafairViewController -随iOS9新推出的控制器。相对于内嵌全功能的Safair浏览器,而不需要切换。

 

1.1Safair

  在Safair中打开网页,点击某个按钮或者区块时,让app切换至Safair打开网页。使用openURL方法:

  UIApplication.sharedApplication().openURL(url)

  

func 点击切换Safair打开网页(){
        if let url = NSURL(string:"http://www.baidu.com"){
            UIApplicaiton.sharedApplication().openURL(url)
            }
        }

 

1.2UIWebView

  用Safair打开网页非常简单。UIWebView相对多一点工作。

  UIWebView既可以打开远程网址,也可以打开app自带的HTML文件。使用它的loadRequest方法即可。

  

1.21使用方法:

  step1:建立新的VC

  建立一个新的ViewController,并且将web view覆盖VC的view(约束提示:宇父视图等宽/等高/水平和垂直居中(不可以用上下左右边距为0的约束)。)当点击某个按钮或者区域时切换到这个视图打开网页。

  step2:将webview的viewdidload加入加载网页代码,实现进入新VC,webview即开始实现加载网页

  1)把web view控件跟代码关联:

  @IBOutlet weak var web: UIWebView!

 

  2)在viewDidLoad中加入:

  

import UIKit

class WebViewController: UIViewController {

    @IBOutlet weak var Web: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        Web.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.baidu.com")!))

        // Do any additional setup after loading the view.
    }

 

  step3:建立手工转场(假设我们是有tableview某个区块实现点击转场)

  按住Ctrl拖动主视图列表的视图控制器到新视图控制器,创建一个show转场,转场identifier命名goWeb,使用点击实现转场:

  

  performSegueWithIdentifier(String:_,sender:self)

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        switch indexPath.section{
        case 1:
            if indexPath.row == 1{
                performSegueWithIdentifier("goWeb", sender: self)
            }
        default:break
            }
        }

 

   step4:运行

  在运行的时候会出现这么一段日志:

  App Transport Security has blocked a cleartext HTTP (http://)resource load since it is insecure.Temporary exceptions can be configured via your app's Info.plist file.

  这是因为App Transport Security 是iOS9的一项安全机制,默认强制所有连接必须为安全链接即https协议。要想使用http链接,需要手工在info.plist里关闭ATS

  step1:打开info.plist,选中add row增加App Transport Security Settings.

  step2:在ATSS列表下找到Allow Arbitrary Loads选择yes

 

1.3SFSafairViewController

  SFSafairViewController具有跟Safair一样的特性比如自动填充和阅读器。

  使用场合:如要集成一个完整的浏览器,推荐使用

  方法:let sfVC = SFSafariViewController(URL:url,entersReaderlfAvailable:true)

 

1.31使用方法:

  step1:打开主视图的代码页导入:import SafariServices

  step2:利用模态展示控制器:presentViewController

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        switch indexPath.section{
        //Safari跳转
        case 0:
            if indexPath.row == 0{
                if let url = NSURL(string: "http://www.baidu.com"){
                    UIApplication.sharedApplication().openURL(url)
                }
                
            }
        //WebView跳转
        case 1:
            if indexPath.row == 1{
                performSegueWithIdentifier("goWeb", sender: self)
            }
        //SFSafariViewController跳转
        case 2:
            if let url = NSURL(string: "http://www.baidu.com"){
                let sfVC = SFSafariViewController(URL: url, entersReaderIfAvailable: true)
                presentViewController(sfVC, animated: true, completion: nil)
            }
        default:break
            }
            tableView.deselectRowAtIndexPath(indexPath, animated: true)
        }