Swift 3: Полный пример UIWebView для перемещения представления в стек и открытия другого html-файла в связке при нажатии на ссылку html, например. <a href="imprint.html">imprint</a>
class WebViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
var filename:String?
override func viewDidLoad() {
super.viewDidLoad()
guard filename != nil else {
fatalError("filename not defined")
}
view.backgroundColor = UIColor.white
webView.isOpaque = false;
webView.delegate = self
webView.backgroundColor = UIColor.clear
//remove file extension first
filename = filename!.replace(".html", replacement: "")
let localfilePath = Bundle.main.url(forResource: filename, withExtension: "html")
let myRequest = NSURLRequest(url: localfilePath!)
webView.loadRequest(myRequest as URLRequest)
}
...
}
extension WebViewController: UIWebViewDelegate {
func webViewDidFinishLoad(_ webView: UIWebView) {
//set view title from html document
let pageTitle = webView.stringByEvaluatingJavaScript(from: "document.title")
navigationItem.title = pageTitle
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .linkClicked,
let urlStr = request.url?.absoluteString,
!urlStr.contains("http://"),
let filename = request.url?.lastPathComponent, //e.g. imprint.html
let vc = storyboard?.instantiateViewController(withIdentifier: "WebView") as? WebViewController{
vc.filename = filename
self.navigationController?.pushViewController(vc, animated: true)
return false
}
return true
}
}