Skip to content

Commit

Permalink
[Share Extension] Support images/movies from other apps like iMessage (
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyok authored Sep 27, 2024
1 parent 77c4a99 commit dd1944e
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions modules/Share-with-Bluesky/ShareViewController.swift
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit dd1944e

Please sign in to comment.