Swift - WKWebview需要花费大量时间来加载url

时间:2017-10-16 19:52:03

标签: swift performance load wkwebview

我的移动应用使用Swift 3 / Xcode 8.3.2和iOS 10.我使用webview / WKWebView在移动应用中呈现网站。因为Xcode没有提供插入wkwebview的功能,所以我使用Xcode关联webview,然后以编程方式为其分配WKWebview并在其中呈现网站。当我使用WIFI(1秒)时网页加载速度非常快,但使用互联网(移动网络)时大约需要10秒。任何线索?

class WebSiteViewController:UIViewController,WKNavigationDelegate,WKUIDelegate {

var webURL: String? = nil
var alertRequestURL: String? = nil
var alertHeader: String? = nil
var websiteView: WKWebView!
var spinner: UIActivityIndicatorView!
@IBOutlet weak var webView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.interactivePopGestureRecognizer?.isEnabled = false    
    let backButton = UIBarButtonItem()
    //self.navigationController?.navigationBar.backItem?.title = ""
    //self.navigationController?.navigationBar.topItem?.title = ""
    //self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton

    if let url = URL(string: webURL!) {
      let request = URLRequest(url:url)
      websiteView.load(request)
    }

    self.spinner  = UIActivityIndicatorView()
    self.spinner.center = CGPoint(x: UIScreen.main.bounds.width/2, y: UIScreen.main.bounds.height/2)
    self.spinner.activityIndicatorViewStyle = .gray
    self.spinner.hidesWhenStopped = true
    self.view.addSubview(spinner)
    self.spinner.startAnimating()
}

override func loadView() {
    super.loadView()
    self.websiteView = WKWebView(frame: self.webView.frame)
    self.websiteView.uiDelegate = self
    self.websiteView.navigationDelegate = self
    self.view = self.websiteView
}

0 个答案:

没有答案