Xcode, Swift; Detect hyperlink click in UIWebView

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 = ""

    override func viewDidLoad() {

        let url: NSURL = NSURL(string: weburl)!
        let requestURL: NSURLRequest = NSURLRequest(URL: url)
    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 ( 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?


  • 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 {
                return false
        return true

    Many thanks to danhhgl from!