I am writing an action extension to transform an image URL and copy to clipboard by clicking the share button of an image on a webpage in Safari.
I created an action extension in Xcode without UI.
In javascript preprocessing script action.js
, I can get the URL and title of the whole page.
run: function(arguments) {
arguments.completionFunction({
"URL" : document.URL,
"title": document.title
})
},
And that works for the share button of the whole webpage.
But how can I get the URL of the image that is pushed one?
CodePudding user response:
When you use from URL, its better to use from :
pod 'SDWebImage'
this is better because of cache
you can Set your URL string with below code:
libraryImageView.sd_setImage(with: URL(string: "your URL string"),
placeholderImage: UIImage(named: Image.placeholder.rawValue))
after that you can get URL with below code:
libraryImageView.sd_ImageURL
CodePudding user response:
Answering my own question. I had to create an action extension with User Interface so that I am working within ActionViewController: UIViewController
class. The trick is to look for UTType.url.identifier
through the shared items.
The code below works for share button of the whole webpage and an image.
override func viewDidLoad() {
super.viewDidLoad()
// search for url
var found = false
for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
for provider in item.attachments! {
if provider.hasItemConformingToTypeIdentifier(UTType.url.identifier) {
provider.loadItem(forTypeIdentifier: UTType.url.identifier, options: nil, completionHandler: { (url, error) in
OperationQueue.main.addOperation {
if let url = url as? URL {
// do something with the url here
}
}})
found = true
break
}
}
if (found) {
// We only handle one url, so stop looking for more.
break
}
}
// don't show user interface
self.done()
}