Learn how to add web views into your apps to display web pages.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3990-your-first-swift-4-ios-11-app/lessons/42
Learn how to add web views into your apps to display web pages.
One interesting thing I noticed. When I drag and drop âBullsEye.htmlâ, the html file was not read in âAboutViewControllerâ. However if I Right Click and Select > âAdd Files To Projectâ the html file has been detected. Just in case someone tries to trouble shoot this issue, this small trick is a time saverâŠ
It appears that WebView is deprecated. Should we be using WebKitView?
I had the same issue. Drage and drop definitely does not work.
Running into the same issue, I am now trying to use WKWebView instead but it seems like its breaking at this line,
if let url = Bundle.main.url(forResource: "BullsEye", withExtension: "html")
Hi, I found this solution
/** The issue is that the file isnât being coping to your app bundle. To fix it:
click your project
Click your target
Select Build Phases
Expand Copy Bundle Resources
Click â+â and select your file.
**/
let url = Bundle.main.url(forResource: âBullsEyeâ, withExtension: âhtmlâ)
let htmlData = try? Data(contentsOf: url!)
let baseURL = URL(fileURLWithPath: Bundle.main.bundlePath)
webView.load(htmlData!, mimeType: âtext/htmlâ, characterEncodingName: âUTF-8â, baseURL: baseURL)
It did work for me. Thanks a lot!!!
Excellent. That worked for me as well. Thank you.
Hmm I have tried all of the above but my webView is still not loading the html file. Does anyone know what the problem could be? Thanks.
Actually, never mind. I had the webView code in the wrong place. I should have put it in viewDidLoad. oops
explicitly importing WebKit for WKWebView does the trick
This topic was automatically closed after 166 days. New replies are no longer allowed.
Continuing the discussion from Your First Swift 4 & iOS 11 App - Part 42: | Ray Wenderlich:
Iâm having a fatal error on Thread 1: Unexpectedly found nil while unwrapping an Optional Value.
I reverted the error using WKWebView just like here:
https://developer.apple.com/documentation/webkit/wkwebview?changes=_8
My problem now is that the close and about autor buttons disappeared.
My AboutViewController.swift code:
import UIKit
import WebKit
class AboutViewController: UIViewController, WKUIDelegate {
@IBOutlet weak var webView: WKWebView!
@IBAction func close() {
dismiss(animated: true, completion: nil)
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
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)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Hi everybody !
I did in a simpler way that worked, see:
First I created an Outlet referencing the WebKitView and then created the following code:
@IBOutlet weak var webView: WKWebView!
Inside viewDidLoad i put this:
let htmlPath = Bundle.main.path(forResource: âBullsEyeâ, ofType: âhtmlâ)
let url = URL(fileURLWithPath: htmlPath!)
let request = URLRequest(url: url)
webView.load(request)
Thatâs allâŠ
Your code not working, Iâll just skip the webview part and continue with the course
Iâve done the same thing, just skipped. My app is working beside this feature.
We have an updated version of the course coming soon that fixes this issue. In the meantime, skipping this and continuing on seems reasonable :]
Yes, agreed. The app is working flawlessly except for that âwebHTMLâ bug.