Wkwebview 100%丢包预设

我有一个异步调用,用于在新的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))
                    }
                })
        })
    }

直到出现空白屏幕,然后出现重试选项。如何减少这种延迟以拥有更好的用户体验。

xiaomading 回答:Wkwebview 100%丢包预设

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()
        }
    }
}
本文链接:https://www.f2er.com/3126483.html

大家都在问