Skip to content

Commit fb62ed9

Browse files
committed
Add OS class
1 parent d8e1ec6 commit fb62ed9

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/SDK/Language/Apple.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ public function getFiles(): array
201201
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/Client.swift',
202202
'template' => '/apple/Sources/Client.swift.twig',
203203
],
204+
[
205+
'scope' => 'default',
206+
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/OS.swift',
207+
'template' => '/apple/Sources/OS.swift.twig',
208+
],
204209
[
205210
'scope' => 'default',
206211
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/OAuth/WebAuthComponent.swift',

templates/apple/Sources/OS.swift.twig

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

0 commit comments

Comments
 (0)