Xcode,斯威夫特;检测 UIWebView 中的超链接点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42108089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:46:30  来源:igfitidea点击:

Xcode, Swift; Detect hyperlink click in UIWebView

iosxcodehyperlinkuiwebviewshouldstartload

提问by Emel Elias

I made an iOS app with Xcode and Swift.

我用 Xcode 和 Swift 制作了一个 iOS 应用程序。

I want to open specific hyperlinks in Safari browser, there others in the WebView itself.

我想在 Safari 浏览器中打开特定的超链接,在 WebView 本身还有其他的。

To reach this, I'll have to check when an hyperlinks gets clicked.

为了达到这个目的,我必须检查超链接何时被点击。

This is the code I have so far:

这是我到目前为止的代码:

//
//  PushViewController.swift
//  App
//
//

import UIKit
class PushViewController: UIViewController, UIWebViewDelegate {
    @IBOutlet var openpushmessage: UIWebView!

    var weburl:String = "http://www.example.com"

    override func viewDidLoad() {
        super.viewDidLoad()

        let url: NSURL = NSURL(string: weburl)!
        let requestURL: NSURLRequest = NSURLRequest(URL: url)
        openpushmessage.loadRequest(requestURL)
    }
    override func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if navigationType == .linkClicked
        {
            print("You clicked a hypelink!")
        }
        return true
    }
}

The code in viewDidLoad()opens the loads the main URL (http://www.example.com) from my server. That works fine.

中的代码viewDidLoad()打开从我的服务器加载主 URL ( http://www.example.com)。这很好用。

override fun webView[…]should detect if a hyperlink is clicked and then print("You clicked a hypelink!").

override fun webView[…]应该检测是否点击了超链接,然后print("You clicked a hypelink!").

But unfortunately I'm getting the following errors:

但不幸的是,我收到以下错误:

one error and one warning for my code

我的代码的一个错误和一个警告

What am I doing wrong? How to get rid of these errors?

我究竟做错了什么?如何摆脱这些错误?

回答by Emel Elias

please try

请尝试

for swift 3.0

快速 3.0

 public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
        {
         if navigationType == .linkClicked
         {

            }
            return true;
        }

swift 2.2

快速 2.2

internal func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool
    {
        if navigationType == .LinkClicked
        {

        }
        return true;
    }

回答by Emel Elias

This is the working solution (Xcode 7.3.1 and Swift 2.2):

这是有效的解决方案(Xcode 7.3.1 和 Swift 2.2):

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    if request.URL?.query?.rangeOfString("target=external") != nil {
        if let url = request.URL {
            UIApplication.sharedApplication().openURL(url)
            return false
        }
    }
    return true
}

Many thanks to danhhgl from freelancer.com!

非常感谢来自 freelancer.com 的 danhhgl

回答by Sour LeangChhean

I think you can solve with this code:

我想你可以用这个代码解决:

 function reportBackToObjectiveC(string)
 {
 var iframe = document.createElement("iframe");
 iframe.setAttribute("src", "callback://" + string);
 document.documentElement.appendChild(iframe);
 iframe.parentNode.removeChild(iframe);
 iframe = null;
 }

var links = document.getElementsByTagName("a");
for (var i=0; i<links.length; i++) {
links[i].addEventListener("click", function() {
    reportBackToObjectiveC("link-clicked");
}, true);
}

in ios code:

在ios代码中:

if ([[[request URL] scheme] isEqualToString:@"callback"]) {
[self setNavigationLeavingCurrentPage:YES];
return NO;
}

More details you can check from here:

您可以从此处查看更多详细信息: