Skip to content

Commit fd0b6ed

Browse files
committed
Merge branch 'master' of github.com:reactnativecomponent/react-native-netease-im
2 parents 60076fa + 652b785 commit fd0b6ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2143
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import NIM from './NIM';
22
import AppCacheUtil from './AppCacheUtil';
3-
3+
import Permissions from './permissions';
44
export {
55
NIM,
66
AppCacheUtil,
7+
Permissions
78
};
89

910
export default NIM;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// RCTConvert+RNPStatus
3+
// ReactNativePermissions
4+
//
5+
// Created by Yonah Forst on 23/03/16.
6+
// Copyright © 2016 Yonah Forst. All rights reserved.
7+
//
8+
9+
#if __has_include("RCTConvert.h")
10+
#import "RCTConvert.h"
11+
#else
12+
#import <React/RCTConvert.h>
13+
#endif
14+
15+
static NSString* RNPStatusUndetermined = @"undetermined";
16+
static NSString* RNPStatusDenied = @"denied";
17+
static NSString* RNPStatusAuthorized = @"authorized";
18+
static NSString* RNPStatusRestricted = @"restricted";
19+
20+
21+
typedef NS_ENUM(NSInteger, RNPType) {
22+
RNPTypeUnknown,
23+
RNPTypeLocation,
24+
RNPTypeCamera,
25+
RNPTypeMicrophone,
26+
RNPTypePhoto,
27+
RNPTypeContacts,
28+
RNPTypeEvent,
29+
RNPTypeReminder,
30+
RNPTypeBluetooth,
31+
RNPTypeNotification,
32+
RNPTypeBackgroundRefresh,
33+
RNPTypeSpeechRecognition
34+
};
35+
36+
@interface RCTConvert (RNPStatus)
37+
38+
@end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// RCTConvert+RNPermissionsStatus.m
3+
// ReactNativePermissions
4+
//
5+
// Created by Yonah Forst on 23/03/16.
6+
// Copyright © 2016 Yonah Forst. All rights reserved.
7+
//
8+
9+
#import "RCTConvert+RNPStatus.h"
10+
11+
@implementation RCTConvert (RNPStatus)
12+
13+
14+
RCT_ENUM_CONVERTER(RNPType, (@{ @"location" : @(RNPTypeLocation),
15+
@"camera" : @(RNPTypeCamera),
16+
@"microphone" : @(RNPTypeMicrophone),
17+
@"photo" : @(RNPTypePhoto),
18+
@"contacts" : @(RNPTypeContacts),
19+
@"event" : @(RNPTypeEvent),
20+
@"reminder" : @(RNPTypeReminder),
21+
@"bluetooth" : @(RNPTypeBluetooth),
22+
@"notification" : @(RNPTypeNotification),
23+
@"backgroundRefresh": @(RNPTypeBackgroundRefresh),
24+
@"speechRecognition": @(RNPTypeSpeechRecognition)
25+
}),
26+
RNPTypeUnknown, integerValue)
27+
28+
@end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// ReactNativePermissions.h
3+
// ReactNativePermissions
4+
//
5+
// Created by Yonah Forst on 18/02/16.
6+
// Copyright © 2016 Yonah Forst. All rights reserved.
7+
//
8+
9+
#if __has_include("RCTBridgeModule.h")
10+
#import "RCTBridgeModule.h"
11+
#else
12+
#import <React/RCTBridgeModule.h>
13+
#endif
14+
15+
@interface ReactNativePermissions : NSObject <RCTBridgeModule>
16+
17+
18+
@end
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
//
2+
// ReactNativePermissions.m
3+
// ReactNativePermissions
4+
//
5+
// Created by Yonah Forst on 18/02/16.
6+
// Copyright © 2016 Yonah Forst. All rights reserved.
7+
//
8+
9+
@import Contacts;
10+
11+
#import "ReactNativePermissions.h"
12+
13+
#if __has_include(<React/RCTBridge.h>)
14+
#import <React/RCTBridge.h>
15+
#elif __has_include("RCTBridge.h")
16+
#import "RCTBridge.h"
17+
#else
18+
#import "React/RCTBridge.h"
19+
#endif
20+
21+
22+
#if __has_include("RCTConvert.h")
23+
#import "RCTConvert.h"
24+
#else
25+
#import <React/RCTConvert.h>
26+
#endif
27+
28+
#if __has_include("RCTEventDispatcher.h")
29+
#import "RCTEventDispatcher.h"
30+
#else
31+
#import <React/RCTEventDispatcher.h>
32+
#endif
33+
34+
#import "RNPLocation.h"
35+
#import "RNPBluetooth.h"
36+
#import "RNPNotification.h"
37+
#import "RNPAudioVideo.h"
38+
#import "RNPEvent.h"
39+
#import "RNPPhoto.h"
40+
#import "RNPContacts.h"
41+
#import "RNPBackgroundRefresh.h"
42+
#import "RNPSpeechRecognition.h"
43+
44+
@interface ReactNativePermissions()
45+
@property (strong, nonatomic) RNPLocation *locationMgr;
46+
@property (strong, nonatomic) RNPNotification *notificationMgr;
47+
@property (strong, nonatomic) RNPBluetooth *bluetoothMgr;
48+
@end
49+
50+
@implementation ReactNativePermissions
51+
52+
53+
RCT_EXPORT_MODULE();
54+
@synthesize bridge = _bridge;
55+
56+
#pragma mark Initialization
57+
58+
- (instancetype)init
59+
{
60+
if (self = [super init]) {
61+
}
62+
63+
return self;
64+
}
65+
66+
/**
67+
* run on the main queue.
68+
*/
69+
- (dispatch_queue_t)methodQueue {
70+
return dispatch_get_main_queue();
71+
}
72+
73+
74+
RCT_REMAP_METHOD(canOpenSettings, canOpenSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
75+
{
76+
resolve(@(UIApplicationOpenSettingsURLString != nil));
77+
}
78+
79+
80+
RCT_EXPORT_METHOD(openSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
81+
{
82+
if (@(UIApplicationOpenSettingsURLString != nil)) {
83+
84+
NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter];
85+
id __block token = [center addObserverForName:UIApplicationDidBecomeActiveNotification
86+
object:nil
87+
queue:nil
88+
usingBlock:^(NSNotification *note) {
89+
[center removeObserver:token];
90+
resolve(@YES);
91+
}];
92+
93+
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
94+
[[UIApplication sharedApplication] openURL:url];
95+
}
96+
}
97+
98+
99+
RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
100+
{
101+
NSString *status;
102+
103+
switch (type) {
104+
105+
case RNPTypeLocation: {
106+
NSString *locationPermissionType = [RCTConvert NSString:json];
107+
status = [RNPLocation getStatusForType:locationPermissionType];
108+
break;
109+
}
110+
case RNPTypeCamera:
111+
status = [RNPAudioVideo getStatus:@"video"];
112+
break;
113+
case RNPTypeMicrophone:
114+
status = [RNPAudioVideo getStatus:@"audio"];
115+
break;
116+
case RNPTypePhoto:
117+
status = [RNPPhoto getStatus];
118+
break;
119+
case RNPTypeContacts:
120+
status = [RNPContacts getStatus];
121+
break;
122+
case RNPTypeEvent:
123+
status = [RNPEvent getStatus:@"event"];
124+
break;
125+
case RNPTypeReminder:
126+
status = [RNPEvent getStatus:@"reminder"];
127+
break;
128+
case RNPTypeBluetooth:
129+
status = [RNPBluetooth getStatus];
130+
break;
131+
case RNPTypeNotification:
132+
status = [RNPNotification getStatus];
133+
break;
134+
case RNPTypeBackgroundRefresh:
135+
status = [RNPBackgroundRefresh getStatus];
136+
break;
137+
case RNPTypeSpeechRecognition:
138+
status = [RNPSpeechRecognition getStatus];
139+
break;
140+
default:
141+
break;
142+
}
143+
144+
resolve(status);
145+
}
146+
147+
RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
148+
{
149+
NSString *status;
150+
151+
switch (type) {
152+
case RNPTypeLocation:
153+
return [self requestLocation:json resolve:resolve];
154+
case RNPTypeCamera:
155+
return [RNPAudioVideo request:@"video" completionHandler:resolve];
156+
case RNPTypeMicrophone:
157+
return [RNPAudioVideo request:@"audio" completionHandler:resolve];
158+
case RNPTypePhoto:
159+
return [RNPPhoto request:resolve];
160+
case RNPTypeContacts:
161+
return [RNPContacts request:resolve];
162+
case RNPTypeEvent:
163+
return [RNPEvent request:@"event" completionHandler:resolve];
164+
case RNPTypeReminder:
165+
return [RNPEvent request:@"reminder" completionHandler:resolve];
166+
case RNPTypeBluetooth:
167+
return [self requestBluetooth:resolve];
168+
case RNPTypeNotification:
169+
return [self requestNotification:json resolve:resolve];
170+
case RNPTypeSpeechRecognition:
171+
return [RNPSpeechRecognition request:resolve];
172+
default:
173+
break;
174+
}
175+
176+
177+
}
178+
179+
- (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve
180+
{
181+
if (self.locationMgr == nil) {
182+
self.locationMgr = [[RNPLocation alloc] init];
183+
}
184+
185+
NSString *type = [RCTConvert NSString:json];
186+
187+
[self.locationMgr request:type completionHandler:resolve];
188+
}
189+
190+
- (void) requestNotification:(id)json resolve:(RCTPromiseResolveBlock)resolve
191+
{
192+
NSArray *typeStrings = [RCTConvert NSArray:json];
193+
194+
UIUserNotificationType types;
195+
if ([typeStrings containsObject:@"alert"])
196+
types = types | UIUserNotificationTypeAlert;
197+
198+
if ([typeStrings containsObject:@"badge"])
199+
types = types | UIUserNotificationTypeBadge;
200+
201+
if ([typeStrings containsObject:@"sound"])
202+
types = types | UIUserNotificationTypeSound;
203+
204+
205+
if (self.notificationMgr == nil) {
206+
self.notificationMgr = [[RNPNotification alloc] init];
207+
}
208+
209+
[self.notificationMgr request:types completionHandler:resolve];
210+
211+
}
212+
213+
214+
- (void) requestBluetooth:(RCTPromiseResolveBlock)resolve
215+
{
216+
if (self.bluetoothMgr == nil) {
217+
self.bluetoothMgr = [[RNPBluetooth alloc] init];
218+
}
219+
220+
[self.bluetoothMgr request:resolve];
221+
}
222+
223+
224+
225+
226+
@end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// RNPAudioVideo.h
3+
// ReactNativePermissions
4+
//
5+
// Created by Yonah Forst on 11/07/16.
6+
// Copyright © 2016 Yonah Forst. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "RCTConvert+RNPStatus.h"
11+
12+
@interface RNPAudioVideo : NSObject
13+
14+
+ (NSString *)getStatus:(NSString *)type;
15+
+ (void)request:(NSString *)type completionHandler:(void (^)(NSString *))completionHandler;
16+
17+
@end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// RNPCamera.m
3+
// ReactNativePermissions
4+
//
5+
// Created by Yonah Forst on 11/07/16.
6+
// Copyright © 2016 Yonah Forst. All rights reserved.
7+
//
8+
9+
#import "RNPAudioVideo.h"
10+
11+
#import <AVFoundation/AVFoundation.h>
12+
13+
@implementation RNPAudioVideo
14+
15+
+ (NSString *)getStatus:(NSString *)type
16+
{
17+
int status = [AVCaptureDevice authorizationStatusForMediaType:[self typeFromString:type]];
18+
switch (status) {
19+
case AVAuthorizationStatusAuthorized:
20+
return RNPStatusAuthorized;
21+
case AVAuthorizationStatusDenied:
22+
return RNPStatusDenied;
23+
case AVAuthorizationStatusRestricted:
24+
return RNPStatusRestricted;
25+
default:
26+
return RNPStatusUndetermined;
27+
}
28+
}
29+
30+
+ (void)request:(NSString *)type completionHandler:(void (^)(NSString *))completionHandler
31+
{
32+
[AVCaptureDevice requestAccessForMediaType:[self typeFromString:type]
33+
completionHandler:^(BOOL granted) {
34+
dispatch_async(dispatch_get_main_queue(), ^{
35+
completionHandler([RNPAudioVideo getStatus:type]);
36+
});
37+
}];
38+
}
39+
40+
+ (NSString *)typeFromString:(NSString *)string {
41+
if ([string isEqualToString:@"audio"]) {
42+
return AVMediaTypeAudio;
43+
} else {
44+
return AVMediaTypeVideo;
45+
}
46+
}
47+
48+
@end

0 commit comments

Comments
 (0)