Group Group Group Group Group Group Group Group Group

Web View deprecated?

In Chapter 7 of iOS Apprentice, we add a Web View, but it appears that this is deprecated in XCode 9. Should this be replaced with a WebKit View, or am I misunderstanding something?

3 Likes

You can still use a Web View for the time being since it will not be removed for at least another iOS release (or maybe even two or three). But yes, it would be better to go with a WebKit View - that was an oversight on my part. We’ll fix it for the next release of the book.

If you do go with a WKWebView, do note that the code in AboutViewController would have to change too since the load method for WKWebView is not quite the same as UIWebView. So, if you can figure out what you need to do, you can still try out using a WebKit View. Otherwise, probably best to stick with the Web View for the time being.

1 Like

Thanks for the quick reply!

I went ahead and used WKWebView - all I had to do was:

  • Import WebKit

  • Replace the UIWebView @IBOutlet with WKWebView

  • Paste in the code from Ch. 7.

  • Accept XCode’s suggested autofix:
    textEncodingName -> characterEncodingName.

I used this as reference:
https://developer.apple.com/documentation/webkit/wkwebview

//
//  AboutViewController.swift
//  BullsEye
//
//  Created by Christopher Clark on 14/10/2017.
//  Copyright © 2017 Christopher Clark. All rights reserved.
//

import UIKit
import WebKit

class AboutViewController: UIViewController {

    @IBOutlet weak var webView: WKWebView!
    
    @IBAction func close() {
        dismiss(animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let url = Bundle.main.url(forResource: "BullsEye",
                                     withExtension: "html") {
            if let htmlData = try? Data(contentsOf: url) {
                let baseURL = URL(fileURLWithPath: Bundle.main.bundlePath)
                webView.load(htmlData, mimeType: "text/html",
                             characterEncodingName: "UTF-8",
                             baseURL: baseURL)
            }
        }
    }
}
6 Likes

I’m glad to hear that you were able to sort it out easily :slight_smile: We’ll update the book for the next release.

2 Likes

This topic was automatically closed after 166 days. New replies are no longer allowed.