-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathEXSpeechRecognitionPermissionRequester.swift
42 lines (36 loc) · 1.22 KB
/
EXSpeechRecognitionPermissionRequester.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
import AppTrackingTransparency
import ExpoModulesCore
import Speech
public class EXSpeechRecognitionPermissionRequester: NSObject, EXPermissionsRequester {
static public func permissionType() -> String {
return "speechrecognition"
}
public func requestPermissions(
resolver resolve: @escaping EXPromiseResolveBlock, rejecter reject: EXPromiseRejectBlock
) {
SFSpeechRecognizer.requestAuthorization { status in
if status != .authorized {
resolve(self.getPermissions())
return
}
AVAudioSession.sharedInstance().requestRecordPermission { authorized in
resolve(self.getPermissions())
}
}
}
public func getPermissions() -> [AnyHashable: Any] {
var status: EXPermissionStatus
let recordPermission = AVAudioSession.sharedInstance().recordPermission
let speechPermission = SFSpeechRecognizer.authorizationStatus()
if speechPermission == .authorized && recordPermission == .granted {
status = EXPermissionStatusGranted
} else if speechPermission == .denied || recordPermission == .denied {
status = EXPermissionStatusDenied
} else {
status = EXPermissionStatusUndetermined
}
return [
"status": status.rawValue
]
}
}