-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathShareHandlerIosViewController.swift
More file actions
340 lines (287 loc) · 13 KB
/
ShareHandlerIosViewController.swift
File metadata and controls
340 lines (287 loc) · 13 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// ShareHandlerIosViewController.swift
// Pods
//
// Created by Josh Juncker on 7/7/22.
//
import UIKit
import Social
import MobileCoreServices
import Photos
import Intents
import Contacts
@available(iOS 14.0, *)
@available(iOSApplicationExtension 14.0, *)
open class ShareHandlerIosViewController: UIViewController {
static var hostAppBundleIdentifier = ""
static var appGroupId = ""
let sharedKey = "ShareKey"
var sharedText: [String] = []
let imageContentType = UTType.image.identifier
let movieContentType = UTType.movie.identifier
let textContentType = UTType.text.identifier
let urlContentType = UTType.url.identifier
let fileURLType = UTType.fileURL.identifier
let dataContentType = UTType.data.identifier
var sharedAttachments: [SharedAttachment] = []
private var fileNameCounter: [String: Int] = [:]
lazy var userDefaults: UserDefaults = {
return UserDefaults(suiteName: ShareHandlerIosViewController.appGroupId)!
}()
public func loadIds() {
// loading Share extension App Id
let shareExtensionAppBundleIdentifier = Bundle.main.bundleIdentifier!;
// convert ShareExtension id to host app id
// By default it is remove last part of id after last point
// For example: com.test.ShareExtension -> com.test
let lastIndexOfPoint = shareExtensionAppBundleIdentifier.lastIndex(of: ".");
ShareHandlerIosViewController.hostAppBundleIdentifier = String(shareExtensionAppBundleIdentifier[..<lastIndexOfPoint!]);
// loading custom AppGroupId from Build Settings or use group.<hostAppBundleIdentifier>
ShareHandlerIosViewController.appGroupId = (Bundle.main.object(forInfoDictionaryKey: "AppGroupId") as? String) ?? "group.\(ShareHandlerIosViewController.hostAppBundleIdentifier)";
}
public override func viewDidLoad() {
super.viewDidLoad();
// load group and app id from build info
loadIds();
Task {
await handleInputItems()
}
}
public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}
func handleInputItems() async {
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
if let contents = content.attachments {
for (index, attachment) in (contents).enumerated() {
do {
if attachment.hasItemConformingToTypeIdentifier(imageContentType) {
try await handleImages(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(movieContentType) {
try await handleVideos(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(fileURLType){
try await handleFiles(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(urlContentType) {
try await handleUrl(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(textContentType) {
try await handleText(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(dataContentType) {
try await handleData(content: content, attachment: attachment, index: index)
} else {
print("Attachment not handled with registered type identifiers: \(attachment.registeredTypeIdentifiers)")
}
} catch {
self.dismissWithError()
}
}
}
redirectToHostApp(content: content)
}
}
public func getNewFileUrl(fileName: String) -> URL {
let newFileUrl = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: ShareHandlerIosViewController.appGroupId)!
.appendingPathComponent(fileName)
return newFileUrl
}
public func handleText (content: NSExtensionItem, attachment: NSItemProvider, index: Int) async throws {
let data = try await attachment.loadItem(forTypeIdentifier: textContentType, options: nil)
if let item = data as? String {
sharedText.append(item)
} else {
if let d = data as? Data {
do{
let contacts = try CNContactVCardSerialization.contacts(with: d)
for contact in contacts {
let data = try CNContactVCardSerialization.data(with: [contact])
let str = String(data: data, encoding: .utf8)!
sharedText.append(str)
}
} catch {
dismissWithError()
}
} else {
dismissWithError()
}
}
}
public func handleUrl (content: NSExtensionItem, attachment: NSItemProvider, index: Int) async throws {
let data = try await attachment.loadItem(forTypeIdentifier: urlContentType, options: nil)
if let item = data as? URL {
sharedText.append(item.absoluteString)
} else {
dismissWithError()
}
}
public func handleImages (content: NSExtensionItem, attachment: NSItemProvider, index: Int) async throws {
let data = try await attachment.loadItem(forTypeIdentifier: imageContentType, options: nil)
var fileName: String?
var imageData: Data?
var sourceUrl: URL?
if let url = data as? URL {
fileName = getFileName(from: url, type: .image)
sourceUrl = url
} else if let iData = data as? Data {
fileName = UUID().uuidString + ".png"
imageData = iData
} else if let image = data as? UIImage {
fileName = UUID().uuidString + ".png"
imageData = image.pngData()
}
if let _fileName = fileName {
let newFileUrl = getNewFileUrl(fileName: _fileName)
do {
if FileManager.default.fileExists(atPath: newFileUrl.path) {
try FileManager.default.removeItem(at: newFileUrl)
}
} catch {
print("Error removing item")
}
var copied: Bool = false
if let _data = imageData {
copied = FileManager.default.createFile(atPath: newFileUrl.path, contents: _data)
} else if let _sourceUrl = sourceUrl {
copied = copyFile(at: _sourceUrl, to: newFileUrl)
}
if (copied) {
sharedAttachments.append(SharedAttachment.init(path: newFileUrl.absoluteString, type: .image))
} else {
dismissWithError()
return
}
} else {
dismissWithError()
return
}
}
public func handleVideos (content: NSExtensionItem, attachment: NSItemProvider, index: Int) async throws {
let data = try await attachment.loadItem(forTypeIdentifier: movieContentType, options: nil)
if let url = data as? URL {
// Always copy
let fileName = getFileName(from: url, type: .video)
let newFileUrl = getNewFileUrl(fileName: fileName)
let copied = copyFile(at: url, to: newFileUrl)
if(copied) {
sharedAttachments.append(SharedAttachment.init(path: newFileUrl.absoluteString, type: .video))
}
} else {
dismissWithError()
}
}
public func handleFiles (content: NSExtensionItem, attachment: NSItemProvider, index: Int) async throws {
let data = try await attachment.loadItem(forTypeIdentifier: fileURLType, options: nil)
if let url = data as? URL {
// Always copy
let fileName = getFileName(from :url, type: .file)
let newFileUrl = getNewFileUrl(fileName: fileName)
let copied = copyFile(at: url, to: newFileUrl)
if (copied) {
sharedAttachments.append(SharedAttachment.init(path: newFileUrl.absoluteString, type: .file))
}
} else {
dismissWithError()
}
}
public func handleData (content: NSExtensionItem, attachment: NSItemProvider, index: Int) async throws {
let data = try await attachment.loadItem(forTypeIdentifier: dataContentType, options: nil)
if let url = data as? URL {
// Always copy
let fileName = getFileName(from :url, type: .file)
let newFileUrl = getNewFileUrl(fileName: fileName)
let copied = copyFile(at: url, to: newFileUrl)
if (copied) {
sharedAttachments.append(SharedAttachment.init(path: newFileUrl.absoluteString, type: .file))
}
} else {
dismissWithError()
}
}
public func dismissWithError() {
print("[ERROR] Error loading data!")
let alert = UIAlertController(title: "Error", message: "Error loading data", preferredStyle: .alert)
let action = UIAlertAction(title: "Error", style: .cancel) { _ in
self.dismiss(animated: true, completion: nil)
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}
public func redirectToHostApp(content: NSExtensionItem) {
// ids may not loaded yet so we need loadIds here too
loadIds();
let url = URL(string: "ShareMedia-\(ShareHandlerIosViewController.hostAppBundleIdentifier)://\(ShareHandlerIosViewController.hostAppBundleIdentifier)?key=\(sharedKey)")
var responder = self as UIResponder?
let selectorOpenURL = sel_registerName("openURL:")
let intent = self.extensionContext?.intent as? INSendMessageIntent
let conversationIdentifier = intent?.conversationIdentifier
let sender = intent?.sender
let serviceName = intent?.serviceName
let speakableGroupName = intent?.speakableGroupName
let subject = content.userInfo?[NSItemProvider.ItemKey.title] as? String
let sharedMedia = SharedMedia.init(attachments: sharedAttachments, conversationIdentifier: conversationIdentifier, content: sharedText.joined(separator: "\n"), subject: subject, speakableGroupName: speakableGroupName?.spokenPhrase, serviceName: serviceName, senderIdentifier: sender?.contactIdentifier ?? sender?.customIdentifier, imageFilePath: nil)
let json = sharedMedia.toJson()
userDefaults.set(json, forKey: sharedKey)
userDefaults.synchronize()
while (responder != nil) {
if let application = responder as? UIApplication {
if #available(iOS 18.0, *) {
let _ = application.open(url!, options: [:], completionHandler: nil)
} else {
let _ = application.perform(selectorOpenURL, with: url)
}
}
responder = responder?.next
}
}
enum RedirectType {
case media
case text
case file
}
func getExtension(from url: URL, type: SharedAttachmentType) -> String {
let parts = url.lastPathComponent.components(separatedBy: ".")
var ex: String? = nil
if (parts.count > 1) {
ex = parts.last
}
if (ex == nil) {
switch type {
case .image:
ex = "PNG"
case .video:
ex = "MP4"
case .file:
ex = "TXT"
default:
ex = ""
}
}
return ex ?? "Unknown"
}
func getFileName(from url: URL, type: SharedAttachmentType) -> String {
var name = url.lastPathComponent
if (name.isEmpty) {
name = UUID().uuidString + "." + getExtension(from: url, type: type)
}
if let count = fileNameCounter[name] {
fileNameCounter[name] = count + 1
name = "\((name as NSString).deletingPathExtension)_\(count + 1).\((name as NSString).pathExtension)"
} else {
fileNameCounter[name] = 1
}
return name
}
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
}
}