[iOS] Swift Detect hyperlink click in UIWebView

有問題的範例:

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

 

正確範例:

public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
{
    if navigationType == .linkClicked
    {
        if let url_text = request.url?.absoluteURL {
            print("linkClicked:", url_text)
        }
    }
    return true;
}

 

相關文章

寫留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *