-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionViewController.swift
133 lines (110 loc) · 4.53 KB
/
ActionViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// ActionViewController.swift
// import_photo_action
//
// Created by NOSSE DJOU Steve on 20/05/2023.
//
import UIKit
import MobileCoreServices
import UniformTypeIdentifiers
class ActionViewController: UIViewController {
var hostAppBundleIdentifier = "com.example.actionExtensionExample"
let sharedKey = "ImportKey"
var appGroupId = ""
var imageURL: URL?
var importedMedia: [ImportedFile] = []
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
loadIds()
var imageFound = false
for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
for provider in item.attachments! {
if provider.hasItemConformingToTypeIdentifier(UTType.image.identifier) {
// This is an image. We'll load it, then place it in our image view.
weak var weakImageView = self.imageView
provider.loadItem(forTypeIdentifier: UTType.image.identifier, options: nil, completionHandler: { (imageURL, error) in
OperationQueue.main.addOperation {
if let strongImageView = weakImageView {
if let imageURL = imageURL as? URL {
self.imageURL = imageURL
strongImageView.image = UIImage(data: try! Data(contentsOf: imageURL))
}
}
}
})
imageFound = true
break
}
}
if (imageFound) {
break
}
}
}
@IBAction func done() {
guard let url = self.imageURL else {
self.extensionContext!.completeRequest(returningItems: self.extensionContext!.inputItems, completionHandler: nil)
return
}
let fileName = "\(url.deletingPathExtension().lastPathComponent).\(url.pathExtension)"
let newPath = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: self.appGroupId)!
.appendingPathComponent(fileName)
let copied = self.copyFile(at: url, to: newPath)
if (copied) {
// TODO: get size
let sharedFile = ImportedFile(
path: newPath.absoluteString,
name: fileName
)
self.importedMedia.append(sharedFile)
}
let userDefaults = UserDefaults(suiteName: self.appGroupId)
userDefaults?.set(self.toData(data: self.importedMedia), forKey: self.sharedKey)
userDefaults?.synchronize()
self.redirectToHostApp()
}
private func redirectToHostApp() {
let url = URL(string: "ImportMedia-\(hostAppBundleIdentifier)://dataUrl=\(sharedKey)")
var responder = self as UIResponder?
let selectorOpenURL = sel_registerName("openURL:")
while (responder != nil) {
if (responder?.responds(to: selectorOpenURL))! {
let _ = responder?.perform(selectorOpenURL, with: url)
}
responder = responder!.next
}
extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}
private func loadIds() {
let shareExtensionAppBundleIdentifier = Bundle.main.bundleIdentifier!;
let lastIndexOfPoint = shareExtensionAppBundleIdentifier.lastIndex(of: ".");
hostAppBundleIdentifier = String(shareExtensionAppBundleIdentifier[..<lastIndexOfPoint!]);
appGroupId = (Bundle.main.object(forInfoDictionaryKey: "AppGroupId") as? String) ?? "group.\(hostAppBundleIdentifier)";
}
func toData(data: [ImportedFile]) -> Data {
let encodedData = try? JSONEncoder().encode(data)
return encodedData!
}
func copyFile(at srcURL: URL, to dstURL: URL) -> Bool {
do {
if FileManager.default.fileExists(atPath: dstURL.path) {
try FileManager.default.removeItem(at: dstURL)
}
try FileManager.default.copyItem(at: srcURL, to: dstURL)
} catch (let error) {
print("Cannot copy item at \(srcURL) to \(dstURL): \(error)")
return false
}
return true
}
}
class ImportedFile: Codable {
var path: String;
var name: String;
init(path: String, name: String) {
self.path = path
self.name = name
}
}