Xcode, Swift; Hyperlink Detection in UIWebView

I made an iOS application with Xcode and Swift.

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

To achieve this, I will need to check when the hyperlink is clicked.

This is the code that I still have:

//
//  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 downloading the main URL ( http://www.example.com ) from my server. It works great.

override fun webView[…]You must determine if the hyperlink is clicked, and then print("You clicked a hypelink!").

But unfortunately, I get the following errors:

one error and one warning for my code

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

+4
source share
3 answers

(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
}

danhhgl freelancer.com!

+4

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;
    }
+6

I think you can solve 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;
}

You can find out more information from here :

0
source

Source: https://habr.com/ru/post/1669122/


All Articles