Xcode, Swift; Detect hyperlink click in UIWebView

大憨熊 提交于 2019-12-04 01:30:37

问题


I made an iOS app with Xcode and Swift.

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

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.

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

But unfortunately I'm getting the following errors:

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


回答1:


please try

for swift 3.0

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

            }
            return true;
        }

swift 2.2

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

        }
        return true;
    }



回答2:


This is the working solution (Xcode 7.3.1 and 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!




回答3:


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:

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

More details you can check from here:



来源:https://stackoverflow.com/questions/42108089/xcode-swift-detect-hyperlink-click-in-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!