5

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:

one error and one warning for my code

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

  • Did you try cleaning the project ? Also remove the override keyword. – Priyal Feb 08 '17 at 08:34
  • @Priyal Okay, I removed `override` and I cleaned the project but still the same. I'm now additionally getting: `Use of undeclared type 'URLRequest'` –  Feb 08 '17 at 08:35
  • I copied your exact code. Resolved the suggestions after which final code is : `func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool { if navigationType == .linkClicked { print("You clicked a hypelink!") } return true }` and it doesn't give any error or warnings. I am using **swift3** so had to resolve warnings. – Priyal Feb 08 '17 at 08:41
  • which swift version are you using ? – Priyal Feb 08 '17 at 08:42
  • I'm still using Swift 2. Any idea what I can do? –  Feb 08 '17 at 08:43
  • Sorry, I am not familiar with swift2 but one thing you can try is to delete your method and copy it from UIWebViewDelegate. Also try comparing with UIWebViewNavigationType.LinkClicked. – Priyal Feb 08 '17 at 08:49

3 Answers3

7

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;
    }
Emel Elias
  • 572
  • 7
  • 18
  • I'm getting `Extraneous '_' in parameter: 'webView' has no keyword argument name` and `Binary operator '==' cannot be applied to operands of type 'UIWebViewNavigationType' and '_'`. –  Feb 08 '17 at 14:15
  • Maybe it's because I'm using Xcode 7.3.1 and Swift 2.2? –  Feb 08 '17 at 15:25
  • That seems to be the problem. Do you know how to convert it to Swift 2.2? –  Feb 09 '17 at 09:02
5

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!

0

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:

Community
  • 1
  • 1
Sour LeangChhean
  • 7,089
  • 6
  • 37
  • 39