代码之家  ›  专栏  ›  技术社区  ›  Daniel T.

WKWebView不会显示我需要的网页

  •  0
  • Daniel T.  · 技术社区  · 5 年前

    给出下面的代码。我找不到网页”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)
        }
    }
    
    0 回复  |  直到 5 年前
        1
  •  -1
  •   ajay negi    5 年前

    WKWebView是降级版本,但webview仍然可以正常工作,我也遇到了同样的问题,我也在做同样的事情,但后来我使用了webview代替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)
            }
            
        }
    
    }