【问题标题】:WKWebView won't display the webpage I need it toWKWebView 不会显示我需要它的网页
【发布时间】:2021-02-06 00:14:58
【问题描述】:

鉴于下面的代码。我无法显示“https://baycare.clearstep.health/covid19”网页。它在 Safari 中显示正常,我可以让其他页面显示在 WKWebView 中。我已经尝试实现所有的导航和 ui 委托方法来尝试追踪问题,但没有找到任何东西。

该 URL 过去可以使用,但该公司已经更改了某些内容,而现在却没有。任何帮助表示赞赏。

下面是一个完整的程序:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let controller = UIViewController()
            .setup { viewController in
                WKWebView(frame: viewController.view.bounds)
                    .setup {
                        viewController.view.addSubview($0)
                        $0.uiDelegate = WebViewUIDelegate.instance
                        $0.navigationDelegate = WebViewDelegate.instance
                        $0.autoresizingMask = [.flexibleWidth, .flexibleHeight]
                        $0.load(URLRequest(url: URL(string: "https://baycare.clearstep.health/covid19")!))
                    }
            }
  
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = controller
        window?.makeKeyAndVisible()

        return true
    }
}

extension NSObjectProtocol {
    @discardableResult
    func setup(_ fn: (Self) -> Void) -> Self {
        fn(self)
        return self
    }
}

final class WebViewUIDelegate: NSObject, WKUIDelegate {
    static let instance = WebViewUIDelegate()
}


final class WebViewDelegate: NSObject, WKNavigationDelegate {

    static let instance = WebViewDelegate()

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print("This doesn't fire so no error?", error)
    }
}

【问题讨论】:

    标签: swift uikit wkwebview


    【解决方案1】:

    WKWebView 是贬低版本,但 webview 仍然可以正常工作,我遇到了同样的问题,我也在做同样的事情,但后来我使用了 web view 而不是 WKWenView

    导入 UIKit 导入 WebKit

    class PolicyVC: UIViewController {
    
        @IBOutlet weak var webView: UIWebView!
        
        var isPolicyView = false
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let button = UIButton(type: UIButton.ButtonType.custom)
            button.setImage(UIImage(named: "ic_arrow_left"), for: .normal)
            button.addTarget(self, action:#selector(popView), for: .touchUpInside)
            button.frame=CGRect(x: 0, y: 0, width: 20, height: 20)
            let barButton = UIBarButtonItem(customView: button)
            self.navigationItem.leftBarButtonItems = [barButton]
            setUpUI()
            
           }
    
        @objc func popView(){
            self.navigationController?.popViewController(animated: true)
        }
        
        override func viewWillAppear(_ animated: Bool) {
           super.viewWillAppear(animated)
        
             self.navigationController?.navigationBar.isTranslucent = true
             self.navigationController?.navigationBar.isHidden = false
             self.navigationController?.navigationBar.backgroundColor = .black
        }
        
        func setUpUI(){
            
            if isPolicyView{
                let url = URL(string: "https://www.termsfeed.com/privacy-policy/68c4cdaeba7e146a7d72bf57654520e7")
                let urlRequest = URLRequest(url: url!)
                webView.loadRequest(urlRequest)
            }else{
                let url = URL(string: "https://www.termsfeed.com/terms-conditions/0001db2c7a1061a55e2f933bb94102de")
                let urlRequest = URLRequest(url: url!)
                webView.loadRequest(urlRequest)
            }
            
        }
    
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多