我正在尝试从我的应用程序打开外部链接,但无法打开

时间:2019-05-26 09:41:01

标签: swift wkwebview

我有一个wkwebview应用程序,我需要代码帮助,我似乎无法在应用程序内部打开网站的外部链接。

import UIKit
import WebKit
import UserNotifications

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
    var webView: WKWebView!
    var activityIndicator: UIActivityIndicatorView!
    var bgImage: UIImageView!
    var urlString = ""



    override func loadView() {
        super.loadView()
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        webView.navigationDelegate = self
        view = webView
    }


    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        if navigationAction.targetFrame == nil {
            let vc = ViewController()
            vc.urlString = navigationAction.request.url?.absoluteString ?? "https://mywebsite"
            vc.view.frame = UIScreen.main.bounds
            vc.webView = WKWebView(frame: UIScreen.main.bounds, configuration: configuration)
            navigationController?.pushViewController(vc, animated: false)
            return vc.webView
        }
        return nil
    }


    override var prefersStatusBarHidden: Bool{
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let image : UIImage = UIImage(named:"bgx1")!
        bgImage = UIImageView(image: image)
        bgImage.frame = CGRect(x: 0, y: 0, width: 1000, height: 2000)
        view.addSubview(bgImage)


        let myURL = URL(string: "https://mywebsite/")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
        webView.allowsBackForwardNavigationGestures = true


        activityIndicator = UIActivityIndicatorView()
        activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)

        self.activityIndicator.center = CGPoint(x:self.view.bounds.size.width/2.0,y: self.view.bounds.size.height/2.0);

        activityIndicator.autoresizingMask = (UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.RawValue(UInt8(UIView.AutoresizingMask.flexibleRightMargin.rawValue) | UInt8(UIView.AutoresizingMask.flexibleLeftMargin.rawValue) | UInt8(UIView.AutoresizingMask.flexibleBottomMargin.rawValue) | UInt8(UIView.AutoresizingMask.flexibleTopMargin.rawValue))))

        activityIndicator.hidesWhenStopped = true

        activityIndicator.style = UIActivityIndicatorView.Style.whiteLarge
        activityIndicator.color = UIColor.darkGray
        self.view.addSubview(activityIndicator)


    }


    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        activityIndicator.startAnimating()
        bgImage.startAnimating()
    }



    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print("It is an error")
        activityIndicator.stopAnimating()
        bgImage.stopAnimating()
        let alert = UIAlertController(title: "Network Error", message: "You have no internet connection", preferredStyle: .alert)

        let restartAction = UIAlertAction(title: "Reload page", style: .default, handler: { (UIAlertAction) in
            self.viewDidLoad()
        })

        alert.addAction(restartAction)
        present(alert, animated: true, completion: nil)
    }


    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        activityIndicator.stopAnimating()
        bgImage.stopAnimating()
        bgImage.isHidden = true
    }

}

我在做什么错?请帮忙。

2 个答案:

答案 0 :(得分:0)

您必须实现WKNavigationDelegate协议的webView(_:decidePolicyFor:decisionHandler:)功能,以便允许在Web视图中跟踪链接。

这里是documentation

答案 1 :(得分:0)

请使用我在图片中显示的该Web视图

enter image description here

代码:-

@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
        super.viewDidLoad()
        webView.delegate = self;
        webView.loadRequest(URLRequest.init(url: URL(string: "https://mywebsite")!));
}

func webViewDidStartLoad(_ webView: UIWebView) {
        SVProgressHUD.setDefaultMaskType(SVProgressHUDMaskType.custom)
        SVProgressHUD.show();
    }

    func webViewDidFinishLoad(_ webView: UIWebView) {
        SVProgressHUD.dismiss();

    }