[Share Extension] Support images/movies from other apps like iMessage (#5515)
This commit is contained in:
@@ -1,5 +1,14 @@
|
|||||||
import UIKit
|
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 {
|
class ShareViewController: UIViewController {
|
||||||
// This allows other forks to use this extension while also changing their
|
// This allows other forks to use this extension while also changing their
|
||||||
// scheme.
|
// scheme.
|
||||||
@@ -43,11 +52,20 @@ class ShareViewController: UIViewController {
|
|||||||
|
|
||||||
private func handleUrl(item: NSItemProvider) async {
|
private func handleUrl(item: NSItemProvider) async {
|
||||||
if let data = try? await item.loadItem(forTypeIdentifier: "public.url") as? URL {
|
if let data = try? await item.loadItem(forTypeIdentifier: "public.url") as? 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),
|
if let encoded = data.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
|
||||||
let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") {
|
let url = URL(string: "\(self.appScheme)://intent/compose?text=\(encoded)") {
|
||||||
_ = self.openURL(url)
|
_ = self.openURL(url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
self.completeRequest()
|
self.completeRequest()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,3 +176,21 @@ class ShareViewController: UIViewController {
|
|||||||
return false
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user