-
Notifications
You must be signed in to change notification settings - Fork 671
Expand file tree
/
Copy pathAppPlugin.swift
More file actions
130 lines (106 loc) · 4.77 KB
/
AppPlugin.swift
File metadata and controls
130 lines (106 loc) · 4.77 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
import Foundation
import Capacitor
@objc(AppPlugin)
public class AppPlugin: CAPPlugin {
private var observers: [NSObjectProtocol] = []
override public func load() {
NotificationCenter.default.addObserver(self, selector: #selector(self.handleUrlOpened(notification:)), name: Notification.Name.capacitorOpenURL, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.handleUniversalLink(notification:)), name: Notification.Name.capacitorOpenUniversalLink, object: nil)
observers.append(NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: OperationQueue.main) { [weak self] (_) in
self?.notifyListeners("appStateChange", data: [
"isActive": true
])
})
observers.append(NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: OperationQueue.main) { [weak self] (_) in
self?.notifyListeners("appStateChange", data: [
"isActive": false
])
})
observers.append(NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: OperationQueue.main) { [weak self] (_) in
self?.notifyListeners("pause", data: nil)
})
observers.append(NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: OperationQueue.main) { [weak self] (_) in
self?.notifyListeners("resume", data: nil)
})
}
deinit {
NotificationCenter.default.removeObserver(self)
for observer in observers {
NotificationCenter.default.removeObserver(observer)
}
}
@objc func handleUrlOpened(notification: NSNotification) {
guard let object = notification.object as? [String: Any?] else {
return
}
notifyListeners("appUrlOpen", data: makeUrlOpenObject(object), retainUntilConsumed: true)
}
@objc func handleUniversalLink(notification: NSNotification) {
guard let object = notification.object as? [String: Any?] else {
return
}
notifyListeners("appUrlOpen", data: makeUrlOpenObject(object), retainUntilConsumed: true)
}
func makeUrlOpenObject(_ object: [String: Any?]) -> JSObject {
guard let url = object["url"] as? NSURL else {
return [:]
}
let options = object["options"] as? [String: Any?] ?? [:]
return [
"url": url.absoluteString ?? "",
"iosSourceApplication": options[UIApplication.OpenURLOptionsKey.sourceApplication.rawValue] as? String ?? "",
"iosOpenInPlace": options[UIApplication.OpenURLOptionsKey.openInPlace.rawValue] as? String ?? ""
]
}
@objc func exitApp(_ call: CAPPluginCall) {
call.unimplemented()
}
@objc func getInfo(_ call: CAPPluginCall) {
if let info = Bundle.main.infoDictionary {
call.resolve([
"name": info["CFBundleDisplayName"] as? String ?? "",
"id": info["CFBundleIdentifier"] as? String ?? "",
"build": info["CFBundleVersion"] as? String ?? "",
"version": info["CFBundleShortVersionString"] as? String ?? ""
])
} else {
call.reject("Unable to get App Info")
}
}
@objc func getLaunchUrl(_ call: CAPPluginCall) {
if let lastUrl = ApplicationDelegateProxy.shared.lastURL {
let urlValue = lastUrl.absoluteString
call.resolve([
"url": urlValue
])
}
call.resolve()
}
@objc func getState(_ call: CAPPluginCall) {
DispatchQueue.main.async {
call.resolve([
"isActive": UIApplication.shared.applicationState == UIApplication.State.active
])
}
}
@objc func minimizeApp(_ call: CAPPluginCall) {
call.unimplemented()
}
@objc func getAppLanguageCode(_ call: CAPPluginCall) {
// According to https://developer.apple.com/news/?id=u2cfuj88, the current
// app language is returned by 'Bundle.main.preferredLocalizations.first'.
// Fallback to device language if app language is not defined.
let appLanguageWithFallbackToDeviceLanguage =
Bundle.main.preferredLocalizations.first ?? Locale.preferredLanguages[0]
let code = String(appLanguageWithFallbackToDeviceLanguage.prefix(2))
call.resolve([
"value": code
])
}
@objc func getAppLanguageTag(_ call: CAPPluginCall) {
let tag = Bundle.main.preferredLocalizations.first ?? Locale.preferredLanguages[0]
call.resolve([
"value": tag
])
}
}