Swift/iOS |如何启用 'press and hold' 以在 UIWebView 中保存图像?

Swift/iOS | How to enable 'press and hold' to save image in a UIWebView?

我正在编写一个应用程序 可以从我的服务器加载文章列表和特定文章。我正在使用 UIWebView 加载文章内容( 是一个 HTML 字符串)。现在的问题是,当用户按住 UIWebView 中的图像时,会跳出一个动作sheet 并给用户保存图像选项。

我尝试了这些,none 对我有用。

whosebug.com/questions/31673916/tap-and-hold-save-image-from-uiwebview

buzztouch.com/forum/thread.php?tid=7EF2CABD19B91FB8D9C20E6&currentPage=1

是的!!!!!!!我终于做到了!!!!通过使用 JavaScript 和 stringByEvaluatingJavaScriptFromString

我在 github 上发布了我的答案:https://github.com/theniceboy/HoldToSaveImage

这是我的代码:

//
//  FrmArticle.swift
//  CenterBrain
//
//  Created by David Chen on 9/7/15.
//  Copyright © 2015 David Chen. All rights reserved.
//

import UIKit

class FrmArticle: UIViewController, UIWebViewDelegate {

    // MARK: - Outlets
    @IBOutlet weak var wvContent: UIWebView!

    // MARK: - Vars & Lets
    var article_id: Int = 1
    var article: Article = Article()

    let kTouchJavaScriptString: String = "document.ontouchstart=function(event){x=event.targetTouches[0].clientX;y=event.targetTouches[0].clientY;document.location=\"myweb:touch:start:\"+x+\":\"+y;};document.ontouchmove=function(event){x=event.targetTouches[0].clientX;y=event.targetTouches[0].clientY;document.location=\"myweb:touch:move:\"+x+\":\"+y;};document.ontouchcancel=function(event){document.location=\"myweb:touch:cancel\";};document.ontouchend=function(event){document.location=\"myweb:touch:end\";};"
    var _gesState: Int = 0
    /*
    _gesState: 
        GESTURE_STATE_NONE = 0,
        GESTURE_STATE_START = 1,
        GESTURE_STATE_MOVE = 2,
        GESTURE_STATE_END = 4
    */

    var _imgURL: String = "", _timer: NSTimer = NSTimer()

    func webView(webView: UIWebView, shouldStartLoadWithRequest _request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if (_request.URL! == "about:blank") {
            return false
        }
        let requestString: String = (_request.URL?.absoluteString)!
        var components: [String] = requestString.componentsSeparatedByString(":")
        if (components.count > 1 && components[0] == "myweb") {
            if (components[1] == "touch") {
                if (components[2] == "start") {
                    _gesState = 1
                    print("touch start!")
                    let ptX: Float = Float(components[3])!
                    let ptY: Float = Float(components[4])!
                    print("touch point \(ptX), \(ptY)")
                    let js: String = "document.elementFromPoint(\(ptX), \(ptY)).tagName"
                    let tagName: String = wvContent.stringByEvaluatingJavaScriptFromString(js)!
                    _imgURL = ""
                    print(tagName)
                    if (tagName == "IMG") {
                        _imgURL = wvContent.stringByEvaluatingJavaScriptFromString("document.elementFromPoint(\(ptX), \(ptY)).src")!
                        _timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "handleLongTouch", userInfo: nil, repeats: false)
                    }
                } else {
                    if (components[2] == "move") {
                        self._gesState = 2
                        print("you are move")
                    } else {
                        if (components[2] == "end") {
                            _timer.invalidate()
                            self._timer = NSTimer()
                            self._gesState = 4
                            print("touch end")
                        }
                    }
                }
            }
            return false
        }
        return true
    }

    func handleLongTouch() {
        print(_imgURL)
        if (_gesState == 1) {
            print("YES!!! I DID IT!!!")
        }
    }

    // MARK: - Override functions
    override func viewDidLoad() {
        wvContent.delegate = self
        super.viewDidLoad()
        wvContent.loadHTMLString("<h2 align=\"center\">Title</h2><img src=\"http://cdn.sstatic.net/Whosebug/img/apple-touch-icon@2.png?v=ea71a5211a91&a\">", baseURL: nil)
    }

    // MARK: - UIWebView delegate
    func webViewDidFinishLoad(webView: UIWebView) {
        activityIndicator.stopAnimating()
        activityIndicator.hidden = true
        wvContent.stringByEvaluatingJavaScriptFromString(kTouchJavaScriptString)
    }
}