diff --git a/modules/Share-with-Bluesky/ShareViewController.swift b/modules/Share-with-Bluesky/ShareViewController.swift index b31f67177..383e8c9ce 100644 --- a/modules/Share-with-Bluesky/ShareViewController.swift +++ b/modules/Share-with-Bluesky/ShareViewController.swift @@ -4,6 +4,7 @@ import AVKit let IMAGE_EXTENSIONS: [String] = ["png", "jpg", "jpeg", "gif", "heic"] let MOVIE_EXTENSIONS: [String] = ["mov", "mp4", "m4v"] let MAX_IMAGES = 10 +let queryValueAllowed = CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~") enum URLType: String, CaseIterable { case image @@ -43,11 +44,9 @@ class ShareViewController: UIViewController { } private func handleText(item: NSItemProvider) async { - 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, + let url = self.composeUrl(param: "text", value: data) { + _ = self.openURL(url) } self.completeRequest() } @@ -62,8 +61,7 @@ class ShareViewController: UIViewController { 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)") { + if let url = self.composeUrl(param: "text", value: data.absoluteString) { _ = self.openURL(url) } } @@ -105,8 +103,7 @@ class ShareViewController: UIViewController { } if valid, - let encoded = imageUris.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), - let url = URL(string: "\(self.appScheme)://intent/compose?imageUris=\(encoded)") { + let url = self.composeUrl(param: "imageUris", value: imageUris) { _ = self.openURL(url) } @@ -118,7 +115,7 @@ class ShareViewController: UIViewController { if let dataUrl = try? await firstItem?.loadItem(forTypeIdentifier: "public.movie") as? URL { if let videoUriInfo = saveVideoWithInfo(dataUrl), - let url = URL(string: "\(self.appScheme)://intent/compose?videoUri=\(videoUriInfo)") { + let url = self.composeUrl(param: "videoUri", value: videoUriInfo) { _ = self.openURL(url) } } @@ -162,6 +159,17 @@ class ShareViewController: UIViewController { return "\(tempUrl.absoluteString)|\(size.width)|\(size.height)" } + /// Builds `://intent/compose?=`. The value is encoded + /// with only RFC 3986 unreserved characters left as-is, because the app + /// parses the link with `URLSearchParams`, which splits on `&`/`=` and reads + /// `+` as a space. + private func composeUrl(param: String, value: String) -> URL? { + guard let encoded = value.addingPercentEncoding(withAllowedCharacters: queryValueAllowed) else { + return nil + } + return URL(string: "\(self.appScheme)://intent/compose?\(param)=\(encoded)") + } + private func completeRequest() { self.extensionContext?.completeRequest(returningItems: nil) }