From dd1944e9b83da248fb50cdd1c10ca18b6c345b87 Mon Sep 17 00:00:00 2001 From: Hailey Date: Thu, 26 Sep 2024 19:57:16 -0700 Subject: [PATCH] [Share Extension] Support images/movies from other apps like iMessage (#5515) --- .../ShareViewController.swift | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/modules/Share-with-Bluesky/ShareViewController.swift b/modules/Share-with-Bluesky/ShareViewController.swift index 46851a0d79..2acbb6187b 100644 --- a/modules/Share-with-Bluesky/ShareViewController.swift +++ b/modules/Share-with-Bluesky/ShareViewController.swift @@ -1,5 +1,14 @@ import UIKit +let IMAGE_EXTENSIONS: [String] = ["png", "jpg", "jpeg", "gif", "heic"] +let MOVIE_EXTENSIONS: [String] = ["mov", "mp4", "m4v"] + +enum URLType: String, CaseIterable { + case image + case movie + case other +} + class ShareViewController: UIViewController { // This allows other forks to use this extension while also changing their // scheme. @@ -43,9 +52,18 @@ class ShareViewController: UIViewController { private func handleUrl(item: NSItemProvider) async { if let data = try? await item.loadItem(forTypeIdentifier: "public.url") as? URL { - if let encoded = data.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), - let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") { - _ = self.openURL(url) + switch data.type { + case .image: + await handleImages(items: [item]) + return + case .movie: + await handleVideos(items: [item]) + return + case .other: + if let encoded = data.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), + let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") { + _ = self.openURL(url) + } } } self.completeRequest() @@ -158,3 +176,21 @@ class ShareViewController: UIViewController { return false } } + +extension URL { + var type: URLType { + get { + guard self.absoluteString.starts(with: "file://"), + let ext = self.pathComponents.last?.split(separator: ".").last?.lowercased() else { + return .other + } + + if IMAGE_EXTENSIONS.contains(ext) { + return .image + } else if MOVIE_EXTENSIONS.contains(ext) { + return .movie + } + return .other + } + } +}