ios - Wkwebview 100%丢包预设

标签 ios swift xcode wkwebview wkwebviewconfiguration

我有一个异步调用来刷新新的 wkwebview 中的 cookie。

 public override func viewDidLoad() {
            super.viewDidLoad()
            let cookies = cookieService.getCookies(forDomain: urlService.getTopLevelDomain())
            authenticationService.authenticateIfNeeded { [weak self] error in
                if let error = error {
                   print(failed)
                } else {

                self?.identityService.storeCookies(cookies) {
                    DispatchQueue.main.async {
                        self?.loadRequest()
                    }
                }
            }
        }

public func authenticateIfNeeded(completion: @escaping (Error?) -> Void) {
       let domain = urlService.getTopLevelDomain()
        identityService.refreshCookies(for: domain, force: true, completion: completion)
    }

我已将网络设置为 100% 丢包率预设。

身份服务中具有 setcookies 的逻辑具有重试选项,完成此重试调用总共需要 60 秒。

func storeCookies(_ cookies: [AnyObject], completion: (() -> Void)? = nil) {

        let group = DispatchGroup()
        let httpCookies = cookies.compactMap { $0 as? HTTPCookie }
        for httpCookie in httpCookies {

            self.cookieStorageService.setCookie(httpCookie)
            group.enter()
            wkCookieStorage.setCookie(httpCookie) {
                group.leave()
            }
        }
        group.notify(queue: .main) {
            completion?()
        }
    }



func refreshCookies(for domain: String, force: Bool, completion: @escaping VoidResultHandler) {
        Retry<Void>.call(
            shouldRetry: self.shouldRetryIdentityOperation,
            handler: completion,
            method: { completion in
                self.identityOperations.refreshCookies(force: force, domain: domain, handler: { result in
                    switch result {
                    case .value(let cookies):
                        self.storeCookies(cookies) {
                            completion(.value(()))
                        }
                    case .error(let error):
                        completion(.error(error))
                    }
                })
        })
    }

直到那时我看到一个空白屏幕,然后我得到一个重试选项。如何减少这种延迟以获得更好的用户体验。

最佳答案

import UIKit
import WebKit

class ViewController: UIViewController {

    private weak var webView: WKWebView!

    public override func viewDidLoad() {
        super.viewDidLoad()
        let webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
        view.addSubview(webView)
        webView.translatesAutoresizingMaskIntoConstraints = false
        webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        view.rightAnchor.constraint(equalTo: webView.rightAnchor).isActive = true
        view.bottomAnchor.constraint(equalTo: webView.bottomAnchor).isActive = true
        self.webView = webView

        checkNetworkSpeed(timeout: 5) { [weak self] error in
            guard let self = self else { return }
            if let error = error { print("!!!! ERROR: \(error)"); return }
            self.webView.load(URLRequest(url: URL(string: "http://google.com")!))
        }
    }

    enum CheckNetworkErrors: Error {
        case noResponse
        case wrongStatusCode(Int)
    }

    public func checkNetworkSpeed(timeout: TimeInterval = 30, completion: ((Error?) -> Void)?) {
        DispatchQueue.global(qos: .default).async {
            let configuration = URLSessionConfiguration.default
            configuration.timeoutIntervalForRequest = timeout
            configuration.timeoutIntervalForResource = timeout
            let session = URLSession(configuration: configuration)
            let url = URL(string: "https://apple.com")

            session.dataTask(with: url!, completionHandler: { data, response, error in
                var responseError: Error?
                defer { DispatchQueue.main.async { completion?(error) } }
                if let error = error { responseError = error; return }
                guard   let httpResponse = response as? HTTPURLResponse else {
                    responseError = CheckNetworkErrors.noResponse
                    return
                }
                guard   (200...299).contains(httpResponse.statusCode) else {
                    responseError = CheckNetworkErrors.wrongStatusCode(httpResponse.statusCode)
                    return
                }
            }).resume()
        }
    }
}

关于ios - Wkwebview 100%丢包预设,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58795722/

相关文章:

ios - 在 UIAlertController 的文本字段中选择文本

ios - 连续多个 NSURLConnection

iOS In App purchase updatedTransactions : cannot distinguish between new purchase and restore

objective-c - CoreText CopyFontsForRequest 收到 mig IPC 错误

ios - 找到纸角

ios - 同时使用 AVComposition 和 AVVideoComposition 时转换错误

ios - 如何在 iOS 中显示持续时间为 2 分钟的远程通知?

ios - 框架的部署目标无效

iphone - 我需要解析推送通知中的有效负载,但是如果用户按游戏图标而不是按通知,如何获取数据?

ios - 在 ViewController 上的 CollectionViewCell 中使用 PageControl