diff --git a/modules/Share-with-Bluesky/Info.plist b/modules/Share-with-Bluesky/Info.plist index 421abb3c41..43f46a5e56 100644 --- a/modules/Share-with-Bluesky/Info.plist +++ b/modules/Share-with-Bluesky/Info.plist @@ -16,6 +16,8 @@ 1 NSExtensionActivationSupportsImageWithMaxCount 10 + NSExtensionActivationSupportsMovieWithMaxCount + 1 NSExtensionPointIdentifier @@ -38,4 +40,4 @@ CFBundleShortVersionString $(MARKETING_VERSION) - \ No newline at end of file + diff --git a/modules/Share-with-Bluesky/ShareViewController.swift b/modules/Share-with-Bluesky/ShareViewController.swift index c045d578fe..63143277a5 100644 --- a/modules/Share-with-Bluesky/ShareViewController.swift +++ b/modules/Share-with-Bluesky/ShareViewController.swift @@ -5,7 +5,6 @@ class ShareViewController: UIViewController { // scheme. let appScheme = Bundle.main.object(forInfoDictionaryKey: "MainAppScheme") as? String ?? "bluesky" - // override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) @@ -24,6 +23,8 @@ class ShareViewController: UIViewController { await self.handleUrl(item: firstAttachment) } else if firstAttachment.hasItemConformingToTypeIdentifier("public.image") { await self.handleImages(items: attachments) + } else if firstAttachment.hasItemConformingToTypeIdentifier("public.video") { + await self.handleVideos(items: attachments) } else { self.completeRequest() } @@ -31,31 +32,23 @@ class ShareViewController: UIViewController { } private func handleText(item: NSItemProvider) async { - do { - if let data = try await item.loadItem(forTypeIdentifier: "public.text") as? String { - if let encoded = data.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), - let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") { - _ = self.openURL(url) - } + if let data = try? await item.loadItem(forTypeIdentifier: "public.text") as? String { + if let encoded = data.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), + let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") { + _ = self.openURL(url) } - self.completeRequest() - } catch { - self.completeRequest() } + self.completeRequest() } private func handleUrl(item: NSItemProvider) async { - do { - 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) - } + 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) } - self.completeRequest() - } catch { - self.completeRequest() } + self.completeRequest() } private func handleImages(items: [NSItemProvider]) async { @@ -105,6 +98,25 @@ class ShareViewController: UIViewController { self.completeRequest() } + private func handleVideos(items: [NSItemProvider]) async { + let firstItem = items.first + + if let dataUri = try? await firstItem?.loadItem(forTypeIdentifier: "public.video") as? URL { + let ext = String(dataUri.lastPathComponent.split(separator: ".").last ?? "mp4") + if let tempUrl = getTempUrl(ext: ext) { + let data = try? Data(contentsOf: dataUri) + try? data?.write(to: tempUrl) + + if let encoded = dataUri.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), + let url = URL(string: "\(self.appScheme)://intent/compose?videoUri=\(encoded)") { + _ = self.openURL(url) + } + } + } + + self.completeRequest() + } + private func saveImageWithInfo(_ image: UIImage?) -> String? { guard let image = image else { return nil @@ -114,27 +126,26 @@ class ShareViewController: UIViewController { // Saving this file to the bundle group's directory lets us access it from // inside of the app. Otherwise, we wouldn't have access even though the // extension does. - if let dir = FileManager() - .containerURL( - forSecurityApplicationGroupIdentifier: "group.app.bsky") { - let filePath = "\(dir.absoluteString)\(ProcessInfo.processInfo.globallyUniqueString).jpeg" - - if let newUri = URL(string: filePath), - let jpegData = image.jpegData(compressionQuality: 1) { - try jpegData.write(to: newUri) - return "\(newUri.absoluteString)|\(image.size.width)|\(image.size.height)" - } + if let tempUrl = getTempUrl(ext: "jpeg"), + let jpegData = image.jpegData(compressionQuality: 1) { + try jpegData.write(to: tempUrl) + return "\(tempUrl.absoluteString)|\(image.size.width)|\(image.size.height)" } - return nil - } catch { - return nil - } + } catch {} + return nil } private func completeRequest() { self.extensionContext?.completeRequest(returningItems: nil) } + private func getTempUrl(ext: String) -> URL? { + if let dir = FileManager().containerURL(forSecurityApplicationGroupIdentifier: "group.app.bsky") { + return URL(string: "\(dir.absoluteString)\(ProcessInfo.processInfo.globallyUniqueString).\(ext)")! + } + return nil + } + @objc func openURL(_ url: URL) -> Bool { var responder: UIResponder? = self while responder != nil {