Sfoglia il codice sorgente

Add iOS speech recognition

Tres Trantham 7 anni fa
parent
commit
9031cf123e

+ 2
- 1
RCTConvert+RNPStatus.h Vedi File

@@ -25,7 +25,8 @@ typedef NS_ENUM(NSInteger, RNPType) {
25 25
     RNPTypeReminder,
26 26
     RNPTypeBluetooth,
27 27
     RNPTypeNotification,
28
-    RNPTypeBackgroundRefresh
28
+    RNPTypeBackgroundRefresh,
29
+    RNPTypeSpeechRecognition
29 30
 };
30 31
 
31 32
 @interface RCTConvert (RNPStatus)

+ 2
- 1
RCTConvert+RNPStatus.m Vedi File

@@ -20,7 +20,8 @@ RCT_ENUM_CONVERTER(RNPType, (@{ @"location" : @(RNPTypeLocation),
20 20
                                 @"reminder" : @(RNPTypeReminder),
21 21
                                 @"bluetooth" : @(RNPTypeBluetooth),
22 22
                                 @"notification" : @(RNPTypeNotification),
23
-                                @"backgroundRefresh": @(RNPTypeBackgroundRefresh)
23
+                                @"backgroundRefresh": @(RNPTypeBackgroundRefresh),
24
+                                @"speechRecognition": @(RNPTypeSpeechRecognition)
24 25
                                 }),
25 26
                                 RNPTypeUnknown, integerValue)
26 27
 

+ 1
- 0
ReactNativePermissions.js Vedi File

@@ -16,6 +16,7 @@ const RNPTypes = {
16 16
 		'bluetooth',
17 17
 		'notification',
18 18
 		'backgroundRefresh', 
19
+		'speechRecognition', 
19 20
 	],
20 21
 	android: [
21 22
 		'location',

+ 6
- 0
ReactNativePermissions.m Vedi File

@@ -22,6 +22,7 @@
22 22
 #import "RNPPhoto.h"
23 23
 #import "RNPContacts.h"
24 24
 #import "RNPBackgroundRefresh.h"
25
+#import "RNPSpeechRecognition.h"
25 26
 
26 27
 @interface ReactNativePermissions()
27 28
 @property (strong, nonatomic) RNPLocation *locationMgr;
@@ -102,6 +103,9 @@ RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type resolve:
102 103
         case RNPTypeBackgroundRefresh:
103 104
             status = [RNPBackgroundRefresh getStatus];
104 105
             break;
106
+        case RNPTypeSpeechRecognition:
107
+            status = [RNPSpeechRecognition getStatus];
108
+            break;
105 109
         default:
106 110
             break;
107 111
     }
@@ -132,6 +136,8 @@ RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json r
132 136
             return [self requestBluetooth:resolve];
133 137
         case RNPTypeNotification:
134 138
             return [self requestNotification:json resolve:resolve];
139
+        case RNPTypeSpeechRecognition:
140
+            return [RNPSpeechRecognition request:resolve];
135 141
         default:
136 142
             break;
137 143
     }

+ 6
- 1
ReactNativePermissions.xcodeproj/project.pbxproj Vedi File

@@ -7,6 +7,7 @@
7 7
 	objects = {
8 8
 
9 9
 /* Begin PBXBuildFile section */
10
+		2894D7D21E25876700401A74 /* RNPSpeechRecognition.m in Sources */ = {isa = PBXBuildFile; fileRef = 2894D7D11E25876700401A74 /* RNPSpeechRecognition.m */; };
10 11
 		9D46283E1D34719100346A5B /* RNPAudioVideo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D46282F1D34719100346A5B /* RNPAudioVideo.m */; };
11 12
 		9D46283F1D34719100346A5B /* RNPBackgroundRefresh.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D4628311D34719100346A5B /* RNPBackgroundRefresh.m */; };
12 13
 		9D4628401D34719100346A5B /* RNPBluetooth.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D4628331D34719100346A5B /* RNPBluetooth.m */; };
@@ -32,7 +33,8 @@
32 33
 /* End PBXCopyFilesBuildPhase section */
33 34
 
34 35
 /* Begin PBXFileReference section */
35
-		9D23B34F1C767B80008B4819 /* libReactNativePermissions.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libReactNativePermissions.a; sourceTree = BUILT_PRODUCTS_DIR; };
36
+		2894D7D01E25876700401A74 /* RNPSpeechRecognition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RNPSpeechRecognition.h; path = permissions/RNPSpeechRecognition.h; sourceTree = SOURCE_ROOT; };
37
+		2894D7D11E25876700401A74 /* RNPSpeechRecognition.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RNPSpeechRecognition.m; path = permissions/RNPSpeechRecognition.m; sourceTree = SOURCE_ROOT; };
36 38
 		9D46282E1D34719100346A5B /* RNPAudioVideo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RNPAudioVideo.h; path = permissions/RNPAudioVideo.h; sourceTree = SOURCE_ROOT; };
37 39
 		9D46282F1D34719100346A5B /* RNPAudioVideo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RNPAudioVideo.m; path = permissions/RNPAudioVideo.m; sourceTree = SOURCE_ROOT; };
38 40
 		9D4628301D34719100346A5B /* RNPBackgroundRefresh.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RNPBackgroundRefresh.h; path = permissions/RNPBackgroundRefresh.h; sourceTree = SOURCE_ROOT; };
@@ -97,6 +99,8 @@
97 99
 		9D4628091D33C1EC00346A5B /* permissions */ = {
98 100
 			isa = PBXGroup;
99 101
 			children = (
102
+				2894D7D01E25876700401A74 /* RNPSpeechRecognition.h */,
103
+				2894D7D11E25876700401A74 /* RNPSpeechRecognition.m */,
100 104
 				9D46282E1D34719100346A5B /* RNPAudioVideo.h */,
101 105
 				9D46282F1D34719100346A5B /* RNPAudioVideo.m */,
102 106
 				9D4628301D34719100346A5B /* RNPBackgroundRefresh.h */,
@@ -175,6 +179,7 @@
175 179
 			files = (
176 180
 				9D46283F1D34719100346A5B /* RNPBackgroundRefresh.m in Sources */,
177 181
 				9D4628451D34719100346A5B /* RNPPhoto.m in Sources */,
182
+				2894D7D21E25876700401A74 /* RNPSpeechRecognition.m in Sources */,
178 183
 				9D4628431D34719100346A5B /* RNPLocation.m in Sources */,
179 184
 				9D46283E1D34719100346A5B /* RNPAudioVideo.m in Sources */,
180 185
 				9D4628401D34719100346A5B /* RNPBluetooth.m in Sources */,

+ 17
- 0
permissions/RNPSpeechRecognition.h Vedi File

@@ -0,0 +1,17 @@
1
+//
2
+//  RNPSpeechRecognition.h
3
+//  ReactNativePermissions
4
+//
5
+//  Created by Tres Trantham on 01/10/17.
6
+//  Copyright © 2017 Tres Trantham. All rights reserved.
7
+//
8
+
9
+#import <Foundation/Foundation.h>
10
+#import "RCTConvert+RNPStatus.h"
11
+
12
+@interface RNPSpeechRecognition : NSObject
13
+
14
++ (NSString *)getStatus;
15
++ (void)request:(void (^)(NSString *))completionHandler;
16
+
17
+@end

+ 45
- 0
permissions/RNPSpeechRecognition.m Vedi File

@@ -0,0 +1,45 @@
1
+//
2
+//  RNPSpeechRecognition.m
3
+//  ReactNativePermissions
4
+//
5
+//  Created by Tres Trantham on 01/10/17.
6
+//  Copyright © 2017 Tres Trantham. All rights reserved.
7
+//
8
+
9
+#import "RNPSpeechRecognition.h"
10
+
11
+#import <AVFoundation/AVFoundation.h>
12
+
13
+@implementation RNPSpeechRecognition
14
+
15
++ (NSString *)getStatus:(NSString *)type
16
+{
17
+
18
+  int status = [SFSpeechRecognizer authorizationStatus];
19
+
20
+  switch (status) {
21
+      case SFSpeechRecognizerAuthorizationStatusAuthorized:
22
+          return RNPStatusAuthorized;
23
+      case SFSpeechRecognizerAuthorizationStatusDenied:
24
+          return RNPStatusDenied;
25
+      case SFSpeechRecognizerAuthorizationStatusRestricted:
26
+          return RNPStatusRestricted;
27
+      default:
28
+          return RNPStatusUndetermined;
29
+  }
30
+}
31
+
32
++ (void)request:(void (^)(NSString *))completionHandler
33
+{
34
+    void (^handler)(void) =  ^(void) {
35
+        dispatch_async(dispatch_get_main_queue(), ^{
36
+            completionHandler([self.class getStatus]);
37
+        });
38
+    };
39
+
40
+    [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
41
+        handler();
42
+    }];
43
+}
44
+
45
+@end