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:
- enter the viewcontroller that contains a wkwebview, start loading: iOS app wkwebview page
- after method
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
done, I see another loading: (.png) - 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:
ios app wkwebview load
website safari load
remove codes about cookie and useragent, same behavior. use a simple url, like , no different.
本文标签:
版权声明:本文标题:javascript - load same url in safari and in an iOS app with wkwebview, wkwebview always takes longer time than safari - Stack Ov 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310820a1934515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论