简体   繁体   English

迅捷3-不会调用declated函数

[英]swift 3 - declated function doesn't get called

I'm learning Switf 3 on Xcode 8 now and I got stuck on a little app which basically just shows a webpage in a webView element. 我现在正在Xcode 8上学习Switf 3 ,我被困在一个小应用程序上,该应用程序基本上只是在webView元素中显示一个网页。 The only addtion function I try to realize is that external urls get open as shared application (eg. Safari). 我尝试实现的唯一添加功能是,外部url作为共享应用程序(例如Safari)打开。

As there are plenty example in SO I think I found all the elements I need to make it working, but somehow the func webView which checks the url doesn't get called. 由于在SO中有很多示例,我想我找到了使它正常工作所需的所有元素,但是以某种方式检查urlfunc webView不会被调用。

Here is my code form the ViewController.swif so far: 到目前为止,这是我的ViewController.swif代码:

import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

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

    // [...]

    func webView(webView: UIWebView!, shouldStartLoadWithRequest request: URLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
        debugPrint("func webView has been called")
        debugPrint(request.url!)
        if navigationType == UIWebViewNavigationType.linkClicked {
            if (request.url!.host! == "stackoverflow.com"){
                return true
            } else {
                //UIApplication.sharedApplication().openURL(request.URL!)
                UIApplication.shared.open(request.url!)
                return false
            }
        }
        return true
    }
}

Where is my logistical mistake? 我的后勤失误在哪里?

Try this 尝试这个

EDIT 编辑

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        debugPrint("func myWebView has been called")
        debugPrint(request.url!)
        if navigationType == UIWebViewNavigationType.linkClicked {
            if (request.url!.host! == "stackoverflow.com"){
                return true
            } else {
                //UIApplication.sharedApplication().openURL(request.URL!)
                UIApplication.shared.open(request.url!)
                return false
            }
        }
        return true
    }

Swift 3中的委托方法的签名是错误的:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM