WKWebView, introduced in iOS 8 and OS X Yosemite which replaces UIWebView in UIKit and WebView in AppKit UI, it is used to open Safari browser outside the app. It loads web pages faster and more efficiently.
So, in this blog we will learn how to Load an iFrame in WKWebView locally in an iOS App.
Let's see the code Example:
import UIKit
import WebKit
let bookData = BookData.sharedInstance
let quizData = QuizData.sharedInstance
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
// implementing web view and giving delegate
let webViewConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// saving it in a local directory
let htmlPath = URL(fileURLWithPath:
Bundle.main.path(
forResource: quizData.questions[quizData.curQuiz][quizData.curQuestion[quizData.curQuiz]].appendix,
ofType: "html",
inDirectory: "Bookshelf/Books/" + bookData.owned[bookData.bookSelected.ownedId].folder + bookData.owned[bookData.bookSelected.ownedId].version + "/quiz/appendix")!)
webView.navigationDelegate = self
webView.loadFileURL(htmlPath, allowingReadAccessTo: htmlPath)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: WKNavigationDelegate {
// When page finishes loading then update ViewController title
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.title = webView.title
}
}
Above code will load an iFrame locally In WKWebView. For any question please feel free to write in comments below.
0 Comment(s)