Reputation: 1680
I'm now writing a app witch can load the article list and also a specific article from my server. I'm using a UIWebView to load the article content (witch is a HTML string). Now the problem is, when the user press and hold an image in the UIWebView, an action sheet will jump out and give user the save image option.
I tried these and none of them works for me.
stackoverflow.com/questions/31522037/tap-and-hold-to-save-image-in-uiwebview
stackoverflow.com/questions/31673916/tap-and-hold-save-image-from-uiwebview
buzztouch.com/forum/thread.php?tid=7EF2CABD19B91FB8D9C20E6¤tPage=1
Upvotes: 1
Views: 2916
Reputation: 1680
YES!!!!!!! I FINALLY DID IT!!!!! By using JavaScript and stringByEvaluatingJavaScriptFromString
.
I posted my answer on github: https://github.com/theniceboy/HoldToSaveImage
Here's my code:
//
// 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/stackoverflow/img/[email protected]?v=ea71a5211a91&a\">", baseURL: nil)
}
// MARK: - UIWebView delegate
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
activityIndicator.hidden = true
wvContent.stringByEvaluatingJavaScriptFromString(kTouchJavaScriptString)
}
}
Upvotes: 3