Check if link pressed in UIWebView

Solutions Collect From Internet About “Check if link pressed in UIWebView”

You can check the URL with making an custom function

func validateUrl (stringURL : NSString) -> Bool {

    var urlRegEx = "((https|http)://)((\\w|-)+)(([.]|[/])((\\w|-)+))+"
    let predicate = NSPredicate(format:"SELF MATCHES %@", argumentArray:[urlRegEx])
    var urlTest = NSPredicate.predicateWithSubstitutionVariables(predicate)

    return predicate.evaluateWithObject(stringURL)
}

It will return true or false. That is Bool

for example :

if (validateUrl("http://google.com")) {
    //will return true
    println("Do Stuff");
}
else
{
    //If it is false then do stuff here.
}

Call this function in

func webView(WebViewNews: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
    if (validateUrl(request.URL().absoluteString())) {
        //if will return true
        println("Do Stuff");
    }
}

Source : https://stackoverflow.com/a/24207852/3202193