Last active
April 17, 2024 17:17
-
-
Save badrinathvm/8544b7a17ab532598e98dfa641d96568 to your computer and use it in GitHub Desktop.
Sharing images via Action extension target
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActionViewController: UIViewController { | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
getImageDataFromExtension() | |
} | |
} | |
extension ActionViewController { | |
private func getImageDataFromExtension() { | |
guard let item = extensionContext?.inputItems.first as? NSExtensionItem, | |
let extensionAttachments = item.attachments else { return } | |
for provider in extensionAttachments { | |
provider.loadItem(forTypeIdentifier: "public.image") { data, _ in | |
if let fileUrl = data as URL { | |
self.open(url: fileUrl) | |
} | |
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil) | |
} else { | |
self.extensionContext?.cancelRequest(withError: ActionError.failedTorCopyTempFile) | |
} | |
} | |
} | |
} | |
private func open(url: URL) { | |
var responder: UIResponder? = self as UIResponder | |
let selector = #selector(openURL(_:)) | |
while responder != nil { | |
if responder!.responds(to: selector) && responder != self { | |
responder!.perform(selector, with: url) | |
return | |
} | |
responder = responder?.next | |
} | |
} | |
@objc | |
private func openURL(_ url: URL) { | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment