File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -201,6 +201,11 @@ public function getFiles(): array
201
201
'destination ' => '/Sources/{{ spec.title | caseUcfirst}}/Client.swift ' ,
202
202
'template ' => '/apple/Sources/Client.swift.twig ' ,
203
203
],
204
+ [
205
+ 'scope ' => 'default ' ,
206
+ 'destination ' => '/Sources/{{ spec.title | caseUcfirst}}/OS.swift ' ,
207
+ 'template ' => '/apple/Sources/OS.swift.twig ' ,
208
+ ],
204
209
[
205
210
'scope ' => 'default ' ,
206
211
'destination ' => '/Sources/{{ spec.title | caseUcfirst}}/OAuth/WebAuthComponent.swift ' ,
Original file line number Diff line number Diff line change
1
+ import Foundation
2
+ import UserNotifications
3
+
4
+ #if os(iOS) || os(tvOS)
5
+ import UIKit
6
+ public typealias Application = UIApplication
7
+ #elseif os(macOS)
8
+ import AppKit
9
+ public typealias Application = NSApplication
10
+ #elseif os(watchOS)
11
+ import WatchKit
12
+ public typealias Application = WKApplication
13
+ #endif
14
+
15
+ public enum OSPermission {
16
+ case notifications
17
+ }
18
+
19
+ public class OS {
20
+ public static func requestPermission(
21
+ _ application: Application,
22
+ _ permission: OSPermission,
23
+ onGranted: @escaping () -> Void = {},
24
+ onDenied: @escaping () -> Void = {}
25
+ ) {
26
+ switch(permission) {
27
+ case .notifications:
28
+ requestNotificationPermission(
29
+ application,
30
+ onGranted: onGranted,
31
+ onDenied: onDenied
32
+ )
33
+ }
34
+ }
35
+
36
+ private static func requestNotificationPermission(
37
+ _ application: Application,
38
+ onGranted: @escaping () -> Void = {},
39
+ onDenied: @escaping () -> Void = {}
40
+ ) {
41
+ let options: UNAuthorizationOptions = [.alert, .badge, .sound]
42
+
43
+ UNUserNotificationCenter.current().requestAuthorization(
44
+ options: options,
45
+ completionHandler: { granted, error in
46
+ DispatchQueue.main.async {
47
+ if (granted) {
48
+ onGranted()
49
+ application.registerForRemoteNotifications()
50
+ }
51
+ else {
52
+ onDenied()
53
+ }
54
+ }
55
+ }
56
+ )
57
+ }
58
+ }
You can’t perform that action at this time.
0 commit comments