開始用Swift開發(fā)iOS 10 - 21 使用WKWebView和SFSafariViewController

上一篇開始用Swift開發(fā)iOS 10 - 20 使用Tab Bar Controller 和 拆分Storyboard學(xué)習(xí)了工具欄和Storyboard的拆分,這一篇學(xué)習(xí)怎么在app中顯示網(wǎng)頁內(nèi)容。由于原書中使用了的網(wǎng)站在國內(nèi)不好訪問,我就用了我的簡書、博客、Github代替??。

設(shè)計(jì)about view

  • 下載圖片拖進(jìn)Assets.xcasset。

  • 打開about.storyboard,拖進(jìn)一個(gè)Image View到table view header,height為190,imageabout-logo,content modeAspect fit。選擇table view cell,identifierCell,styleBasic。

  • 新建AboutTableViewController繼承至UITableViewController,關(guān)聯(lián)about中的table view controller。

  • AboutTableViewController,添加:

      var sectionTitles = ["Leave Feedback", "Follow Us"]
      var sectionContent = [["Rate us on App Store", "Tell us your feedback"], ["Jianshu", "Blog", "Github"]]
      var links = ["http://m.itdecent.cn/u/efce1a2a95ab", "http://andyron.com", "https://github.com/andyRon"]
    

table view的相關(guān)代理協(xié)議方法實(shí)現(xiàn),這次使用倆個(gè)section,tableView(_:titleForHeaderInSection:)是獲取section的title方法。

    override func numberOfSections(in tableView: UITableView) -> Int {
        return sectionTitles.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return sectionContent[section].count
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sectionTitles[section]
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        
        cell.textLabel?.text = sectionContent[indexPath.section][indexPath.row]
        
        return cell
    }
  • 移除Tableview下面分割線,在viewDidLoad中添加:

    tableView.tableFooterView = UIView(frame: CGRect.zero)
    

用Mobile Safari打開Web

可直接給出網(wǎng)址,通過Mobile Safari打開網(wǎng)站,UIApplication.shared.open(_:options:completionHandler:)。

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch indexPath.section {
        case 0:
            if indexPath.row == 0 {
                if let url = URL(string: "http://www.apple.com/itunes/charts/paid-apps/") {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            }
        default:
            break
        }
        // 取消被選中狀態(tài)
        tableView.deselectRow(at: indexPath, animated: false)
    }

使用WKWebView載入Web

使用使用WKWebView載入Web的例子

if let url = URL(string: "http://andyron.com") {
    let request = URLRequest(url: url)
    webView.load(request)
}

直接載入本地html文件的例子:

let url = URL(fileURLWithPath: "about.html")
let request = URLRequest(url: url)
webView.load(request)
  • about.storyboard中拖進(jìn)一個(gè)新的View Controller,用來展示W(wǎng)eb內(nèi)容。使用ctrl-drag建立Show類型的segue,identifiershowWebView
  • 新建WebViewController繼承至UIViewController,關(guān)聯(lián)上面的用來顯示W(wǎng)eb內(nèi)容的View Controller。
  • WebViewController.swift中添加import WebKit,變量var webView: WKWebView!。
  • 更新 viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()

        if let url = URL(string: "http://andyron.com") {
            let request = URLRequest(url: url)
            webView.load(request)
        }
    
    }
  • 添加loadView,這個(gè)方法會(huì)在viewDidLoad之前調(diào)用創(chuàng)建WKWebView。
override func loadView() {
    webView = WKWebView()
    view = webView
}
  • 更新AboutTableViewController中的tableView(_didSelectRowAtIndexPath:)
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch indexPath.section {
        case 0:
            if indexPath.row == 0 {
                if let url = URL(string: "http://www.apple.com/itunes/charts/paid-apps/") {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            } else if indexPath.row == 1 {
                performSegue(withIdentifier: "showWebView", sender: self)
    
            }     
        default:
            break
        }
        // 取消被選中狀態(tài)
        tableView.deselectRow(at: indexPath, animated: false)
    }
  • 由于自從iOS 9之后,出于安全考慮,默認(rèn)只能訪問HTTPS的網(wǎng)站,如果需要訪問HTTP的網(wǎng)站,就需要在plist文件中添加許可:

使用SFSafariViewController載入Web

AboutTableViewController.swift中加入import SafariServices,然后更新tableView(_didSelectRowAtIndexPath:),只用通過url創(chuàng)建一個(gè)SFSafariViewController對(duì)象,然后使用present方法展示就可以了。

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch indexPath.section {
        case 0:
            if indexPath.row == 0 {
                if let url = URL(string: "http://www.apple.com/itunes/charts/paid-apps/") {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            } else if indexPath.row == 1 {
                performSegue(withIdentifier: "showWebView", sender: self)
    
            }
        case 1:
            if let url = URL(string: links[indexPath.row]) {
                let safariController = SFSafariViewController(url: url)
                present(safariController, animated: true, completion: nil)
            }
        default:
            break
        }
        // 取消被選中狀態(tài)
        tableView.deselectRow(at: indexPath, animated: false)
    }

代碼

Beginning-iOS-Programming-with-Swift

說明

此文是學(xué)習(xí)appcode網(wǎng)站出的一本書 《Beginning iOS 10 Programming with Swift》 的一篇記錄

系列文章目錄

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,704評(píng)論 4 61
  • 前兩天,有朋友問了個(gè)問題:如果一個(gè)男人買奶粉的錢都沒有,你會(huì)和他在一起嗎? 在現(xiàn)實(shí)中我們更可能會(huì)愛上和自己差不多的...
    在寫字的路上遇見自己閱讀 222評(píng)論 0 0
  • 人生就像一場旅程,列車的每次??慷紩?huì)上來一波人也會(huì)下去一波人。我們身邊的人卻不過只是過客而已。 雖然我挺認(rèn)同這個(gè)觀...
    漂流晶閱讀 572評(píng)論 0 1
  • 今天在簡書上拜讀了韓大爺?shù)囊黄恼隆赌愕娜烁裢晟瞥啥紱Q定著你的發(fā)展高度》,感慨良久,在此記錄下自己的一些想...
    瘋木頭閱讀 385評(píng)論 0 2
  • NodeSchool 上有一大堆免費(fèi)的課程。其中,Node.js 的入門課程是 Learn you Node。想成...
    知行社閱讀 692評(píng)論 1 13

友情鏈接更多精彩內(nèi)容