/
SharePlugin.swift
55 lines (44 loc) · 1.69 KB
/
SharePlugin.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
import Foundation
import Capacitor
@objc(SharePlugin)
public class SharePlugin: CAPPlugin {
@objc func share(_ call: CAPPluginCall) {
var items = [Any]()
if let text = call.getString("text") {
items.append(text)
}
if let url = call.getString("url"), let urlObj = URL(string: url) {
items.append(urlObj)
}
let title = call.getString("title")
if items.count == 0 {
call.reject("Must provide at least url or text")
return
}
DispatchQueue.main.async { [weak self] in
let actionController = UIActivityViewController(activityItems: items, applicationActivities: nil)
if title != nil {
actionController.setValue(title, forKey: "subject")
}
actionController.completionWithItemsHandler = { (activityType, completed, _ returnedItems, activityError) in
if activityError != nil {
call.reject("Error sharing item", nil, activityError)
return
}
if completed {
call.resolve([
"activityType": activityType?.rawValue ?? ""
])
} else {
call.reject("Share canceled")
}
}
if self?.bridge?.viewController?.presentedViewController != nil {
call.reject("Can't share while sharing is in progress")
return
}
self?.setCenteredPopover(actionController)
self?.bridge?.viewController?.present(actionController, animated: true, completion: nil)
}
}
}