Browse Source

Add iOS speech recognition

Tres Trantham 9 years ago
parent
commit
9031cf123e

+ 2 - 1
RCTConvert+RNPStatus.h

@@ -25,7 +25,8 @@ typedef NS_ENUM(NSInteger, RNPType) {
     RNPTypeReminder,
     RNPTypeBluetooth,
     RNPTypeNotification,
-    RNPTypeBackgroundRefresh
+    RNPTypeBackgroundRefresh,
+    RNPTypeSpeechRecognition
 };
 
 @interface RCTConvert (RNPStatus)

+ 2 - 1
RCTConvert+RNPStatus.m

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

+ 1 - 0
ReactNativePermissions.js

@@ -16,6 +16,7 @@ const RNPTypes = {
 		'bluetooth',
 		'notification',
 		'backgroundRefresh', 
+		'speechRecognition', 
 	],
 	android: [
 		'location',

+ 6 - 0
ReactNativePermissions.m

@@ -22,6 +22,7 @@
 #import "RNPPhoto.h"
 #import "RNPContacts.h"
 #import "RNPBackgroundRefresh.h"
+#import "RNPSpeechRecognition.h"
 
 @interface ReactNativePermissions()
 @property (strong, nonatomic) RNPLocation *locationMgr;
@@ -102,6 +103,9 @@ RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type resolve:
         case RNPTypeBackgroundRefresh:
             status = [RNPBackgroundRefresh getStatus];
             break;
+        case RNPTypeSpeechRecognition:
+            status = [RNPSpeechRecognition getStatus];
+            break;
         default:
             break;
     }
@@ -132,6 +136,8 @@ RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json r
             return [self requestBluetooth:resolve];
         case RNPTypeNotification:
             return [self requestNotification:json resolve:resolve];
+        case RNPTypeSpeechRecognition:
+            return [RNPSpeechRecognition request:resolve];
         default:
             break;
     }

+ 6 - 1
ReactNativePermissions.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		2894D7D21E25876700401A74 /* RNPSpeechRecognition.m in Sources */ = {isa = PBXBuildFile; fileRef = 2894D7D11E25876700401A74 /* RNPSpeechRecognition.m */; };
 		9D46283E1D34719100346A5B /* RNPAudioVideo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D46282F1D34719100346A5B /* RNPAudioVideo.m */; };
 		9D46283F1D34719100346A5B /* RNPBackgroundRefresh.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D4628311D34719100346A5B /* RNPBackgroundRefresh.m */; };
 		9D4628401D34719100346A5B /* RNPBluetooth.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D4628331D34719100346A5B /* RNPBluetooth.m */; };
@@ -32,7 +33,8 @@
 /* End PBXCopyFilesBuildPhase section */
 
 /* Begin PBXFileReference section */
-		9D23B34F1C767B80008B4819 /* libReactNativePermissions.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libReactNativePermissions.a; sourceTree = BUILT_PRODUCTS_DIR; };
+		2894D7D01E25876700401A74 /* RNPSpeechRecognition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RNPSpeechRecognition.h; path = permissions/RNPSpeechRecognition.h; sourceTree = SOURCE_ROOT; };
+		2894D7D11E25876700401A74 /* RNPSpeechRecognition.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RNPSpeechRecognition.m; path = permissions/RNPSpeechRecognition.m; sourceTree = SOURCE_ROOT; };
 		9D46282E1D34719100346A5B /* RNPAudioVideo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RNPAudioVideo.h; path = permissions/RNPAudioVideo.h; sourceTree = SOURCE_ROOT; };
 		9D46282F1D34719100346A5B /* RNPAudioVideo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RNPAudioVideo.m; path = permissions/RNPAudioVideo.m; sourceTree = SOURCE_ROOT; };
 		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 @@
 		9D4628091D33C1EC00346A5B /* permissions */ = {
 			isa = PBXGroup;
 			children = (
+				2894D7D01E25876700401A74 /* RNPSpeechRecognition.h */,
+				2894D7D11E25876700401A74 /* RNPSpeechRecognition.m */,
 				9D46282E1D34719100346A5B /* RNPAudioVideo.h */,
 				9D46282F1D34719100346A5B /* RNPAudioVideo.m */,
 				9D4628301D34719100346A5B /* RNPBackgroundRefresh.h */,
@@ -175,6 +179,7 @@
 			files = (
 				9D46283F1D34719100346A5B /* RNPBackgroundRefresh.m in Sources */,
 				9D4628451D34719100346A5B /* RNPPhoto.m in Sources */,
+				2894D7D21E25876700401A74 /* RNPSpeechRecognition.m in Sources */,
 				9D4628431D34719100346A5B /* RNPLocation.m in Sources */,
 				9D46283E1D34719100346A5B /* RNPAudioVideo.m in Sources */,
 				9D4628401D34719100346A5B /* RNPBluetooth.m in Sources */,

+ 17 - 0
permissions/RNPSpeechRecognition.h

@@ -0,0 +1,17 @@
+//
+//  RNPSpeechRecognition.h
+//  ReactNativePermissions
+//
+//  Created by Tres Trantham on 01/10/17.
+//  Copyright © 2017 Tres Trantham. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "RCTConvert+RNPStatus.h"
+
+@interface RNPSpeechRecognition : NSObject
+
++ (NSString *)getStatus;
++ (void)request:(void (^)(NSString *))completionHandler;
+
+@end

+ 45 - 0
permissions/RNPSpeechRecognition.m

@@ -0,0 +1,45 @@
+//
+//  RNPSpeechRecognition.m
+//  ReactNativePermissions
+//
+//  Created by Tres Trantham on 01/10/17.
+//  Copyright © 2017 Tres Trantham. All rights reserved.
+//
+
+#import "RNPSpeechRecognition.h"
+
+#import <AVFoundation/AVFoundation.h>
+
+@implementation RNPSpeechRecognition
+
++ (NSString *)getStatus:(NSString *)type
+{
+
+  int status = [SFSpeechRecognizer authorizationStatus];
+
+  switch (status) {
+      case SFSpeechRecognizerAuthorizationStatusAuthorized:
+          return RNPStatusAuthorized;
+      case SFSpeechRecognizerAuthorizationStatusDenied:
+          return RNPStatusDenied;
+      case SFSpeechRecognizerAuthorizationStatusRestricted:
+          return RNPStatusRestricted;
+      default:
+          return RNPStatusUndetermined;
+  }
+}
+
++ (void)request:(void (^)(NSString *))completionHandler
+{
+    void (^handler)(void) =  ^(void) {
+        dispatch_async(dispatch_get_main_queue(), ^{
+            completionHandler([self.class getStatus]);
+        });
+    };
+
+    [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
+        handler();
+    }];
+}
+
+@end