Migrate UIWebView to WKWebView

穿精又带淫゛_ 提交于 2019-12-10 09:34:43

问题


Within my iOS app I am attempting to migrate my old UIWebView code to WKWebView since WKWebView is, in theory, faster and more efficient than UIWebView.

I have looked at a bunch of tutorials (like here and here and here) on the internet but cant find anything that explains how to simply add a WKWebView to my app progmatically.

Can someone point me to a simple tutorial or explain in the comments how to convert this code to WKWebView progmatically? thanks in advance.

View Controller.swift:

import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))

        myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.communionchapelefca.org/app-home")!))
        self.view.addSubview(myWebView)

回答1:


Add "WebKit" framework to your class.

Please refer the below code

import UIKit
import WebKit

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let myWebView:WKWebView = WKWebView(frame: CGRectMake(0, 0,   UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))

        myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.communionchapelefca.org/app-home")!))
        self.view.addSubview(myWebView)

    }
}


来源:https://stackoverflow.com/questions/36214247/migrate-uiwebview-to-wkwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!