admin管理员组

文章数量:1122832

we have a website, now I am developing an iOS app that can load the webpage from our website, but I found the same page loaded fast in website, but slow in my iOS app. in my app, I use WKWebView to load url, below is the code:

override func viewDidLoad() {
        super.viewDidLoad()
        if subCatgory == "voa" {
            pre = "NewsArticle"
        } else if subCatgory == "novel" {
            pre = "ShortStoryArticle"
        }

        let token = UserDefaults.standard.value(forKey: "token") ?? ""
        // i need add custom cookie value
        let cookie:HTTPCookie = HTTPCookie(properties: [.domain: XYVar.domain, .path: "/", .name: "session", .value: token])!
        let guestCookie: HTTPCookie = HTTPCookie(properties: [.domain: XYVar.domain, .path: "/", .name: "uuid", .value: User.shared.guestUUID ?? ""])!
        let dataStorage = WKWebsiteDataStore.default()
        dataStorage.httpCookieStore.setCookie(cookie)
        dataStorage.httpCookieStore.setCookie(guestCookie)
        
        let conf = WKWebViewConfiguration()
        let contentController = WKUserContentController()
        contentController.add(self, name: "getAppUseStatus")
        
        conf.userContentController = contentController
        conf.websiteDataStore = dataStorage
        
        webView = WKWebView(frame: CGRect.zero, configuration: conf)
        view.addSubview(webView!)
        webView?.snp.makeConstraints({ make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        })
        
        if #available(iOS 16.4, *) {
            webView?.isInspectable = true
        } else {
            // Fallback on earlier versions
        }
        let url = URL(string: "/\(pre)/\(fileNameForJson)")
        guard let loadURL = url else {
            return
        }
        let request = URLRequest(url: loadURL)
        webView?.navigationDelegate = self
        // add useragent
        webView?.evaluateJavaScript("navigator.userAgent", completionHandler: { [self] result, error in
            let userAgent = result as! String
            let userAgentNew = userAgent + " isApp=true"
            webView?.customUserAgent = userAgentNew
            webView?.load(request)
        })
    }

the below is delegate:

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        // start show a loading ui
        configLoading()
//        print("start------  \(Date())")
    }
    
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
//        print("didCommit------  \(Date())")
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        configureStopLoading()
//        print("end-----  \(Date())")
    }

in iOS app, when start loading, the method

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) start executing, and after several seconds, the method func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) executes. after this method done, the webpage seems start load itself. the below is the page I saw:

  1. enter the viewcontroller that contains a wkwebview, start loading: iOS app wkwebview page
  2. after method func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) done, I see another loading: (.png)
  3. after loading finished, the content shown: content shown

I don't know what happened between the two method: func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) if they have load some necessary resource for the webpage, then why I see another loading.

and, in website, we can only see the step 2,3. so it shows content quickly.

here is the two snapshot about what loaded between iOS app and website:

  1. ios app wkwebview load

  2. website safari load

remove codes about cookie and useragent, same behavior. use a simple url, like , no different.

本文标签: