Xcode,Swift;检测UIWebView中的超链接 [英] Xcode, Swift; Detect hyperlink click in UIWebView

查看:320
本文介绍了Xcode,Swift;检测UIWebView中的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Xcode和Swift创建了一个iOS应用程序。



我想在Safari浏览器中打开特定的超链接,其他人在WebView本身。



要达到此目的,我必须检查点击超链接的时间。



这是我到目前为止的代码:

  // 
// PushViewController.swift
// App
//
//

import UIKit
类PushViewController:UIViewController,UIWebViewDelegate {
@IBOutlet var openpushmessage:UIWebView!

var weburl:String =http://www.example.com

覆盖func viewDidLoad(){
super.viewDidLoad()

let url:NSURL = NSURL(string:weburl)!
let requestURL:NSURLRequest = NSURLRequest(URL:url)
openpushmessage.loadRequest(requestURL)
}
覆盖func webView(_ webView:UIWebView,shouldStartLoadWithRequest request:URLRequest,navigationType: UIWebViewNavigationType) - > Bool {
如果navigationType == .linkClicked
{
print(你点击超链接!)
}
返回true
}
}

中的代码viewDidLoad()从我的服务器打开加载主URL(



我做错了什么?如何摆脱这些错误?

解决方案

这是工作解决方案(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 ()。opennLL(url)
返回false
}
}
返回true
}

非常感谢来自freelancer.com的 danhhgl


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?

解决方案

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!

这篇关于Xcode,Swift;检测UIWebView中的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆