Swift 5 WKWebView застрял под нагрузкой - PullRequest
0 голосов
/ 04 июля 2019

Я новичок в программировании на iOS, сейчас я пытаюсь создать WebView для моего WebApp.Настройка в основном работает, единственная проблема - обработка ссылок target="_blank", потому что они неправильно обрабатываются WebView, так как он ничего не делает.

Поиск в Интернете Я попытался запустить код, найденный ввторой ответ здесь .
Вот что я получил.

//  ViewController.swift
//  SellOutControl
//
//  Created by Gaetano on 17/05/2019.
//  Copyright © 2019 Abacus System. All rights reserved.
//

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate{ //<--added WKUIDelegate here to properly handle the UIDelegate below

    private weak var webView: WKWebView?
    private lazy var url = URL (string: "https://app.selloutcontrol.com")!

    func initWebView(configuration: WKWebViewConfiguration) {
        if webView != nil { return }
        let webView = WKWebView(frame: UIScreen.main.bounds, configuration: configuration)
        webView.uiDelegate = self
        view.addSubview(webView)
        self.webView = webView
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if webView == nil { initWebView(configuration: WKWebViewConfiguration()) }
        webView?.load(url: url)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.


        let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView?.reload))
        let backButton = UIBarButtonItem(title: "<", style: .plain, target: self, action: #selector(goBack) )
        let forwardButton = UIBarButtonItem(title: ">", style: .plain, target: self, action: #selector(goForward) )
        toolbarItems = [backButton, refresh, forwardButton]
        navigationController?.isToolbarHidden = false
    }

    @objc private func goBack() {
        if webView!.canGoBack {
            webView?.goBack()
        } else {
            self.dismiss(animated: true, completion: nil)
        }
    }
    @objc private func goForward() {
        if webView!.canGoForward {
            webView?.goForward()
        } else {
            self.dismiss(animated: true, completion: nil)
        }
    }
}
    extension ViewController{ //<--changed this declaration

    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    // push new screen to the navigation controller when need to open url in another "tab"

        if let url = navigationAction.request.url, navigationAction.targetFrame == nil {
            let viewController = ViewController()
            viewController.initWebView(configuration: configuration)
            viewController.url = url
            DispatchQueue.main.async { [weak self] in
         self?.navigationController?.pushViewController(viewController, animated: true)
            }
            return viewController.webView
        }
        title = webView.url?.host
        return nil
        }
    }

    extension WKWebView {
        func load(url: URL) { load(URLRequest(url: url)) }
    }

С приведенным выше кодом приложение не работает, застряло на главной странице, так как я не могу нажатьили проведите что-нибудь.
Что не так?

...