瀏覽代碼

Store handlers in dictionary to prevent ARC

booker 5 年之前
父節點
當前提交
c02d8766cb
共有 2 個檔案被更改,包括 26 行新增0 行删除
  1. 2
    0
      ios/RNPermissions.h
  2. 24
    0
      ios/RNPermissions.m

+ 2
- 0
ios/RNPermissions.h 查看文件

@@ -79,6 +79,8 @@ typedef enum {
79 79
 
80 80
 @interface RNPermissions : NSObject <RCTBridgeModule>
81 81
 
82
+@property (nonatomic, strong) NSMutableDictionary<NSString *, id<RNPermissionHandler>>  *_Nonnull handlers;
83
+
82 84
 + (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId;
83 85
 
84 86
 + (void)flagAsRequested:(NSString * _Nonnull)handlerId;

+ 24
- 0
ios/RNPermissions.m 查看文件

@@ -225,6 +225,18 @@ RCT_EXPORT_MODULE();
225 225
   }
226 226
 }
227 227
 
228
+- (NSString *)insertHandler:(id<RNPermissionHandler>)handler {
229
+    if( self.handlers == nil){
230
+        self.handlers = [NSMutableDictionary new];
231
+    }
232
+    
233
+    NSString *randomId = [[NSUUID UUID] UUIDString];
234
+    
235
+    [self.handlers setObject:handler forKey:randomId];
236
+    
237
+    return randomId;
238
+}
239
+
228 240
 + (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId {
229 241
   NSArray<NSString *> *requested = [[NSUserDefaults standardUserDefaults] arrayForKey:SETTING_KEY];
230 242
   return requested == nil ? false : [requested containsObject:handlerId];
@@ -271,13 +283,19 @@ RCT_REMAP_METHOD(check,
271 283
                  rejecter:(RCTPromiseRejectBlock)reject) {
272 284
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
273 285
 
286
+  NSString *randomId = [self insertHandler: handler];
287
+
274 288
   [handler checkWithResolver:^(RNPermissionStatus status) {
275 289
     NSString *strStatus = [self stringForStatus:status];
276 290
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
277 291
     resolve(strStatus);
292
+
293
+    [self.handlers removeObjectForKey:randomId];
278 294
   } rejecter:^(NSError *error) {
279 295
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
280 296
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
297
+
298
+    [self.handlers removeObjectForKey:randomId];
281 299
   }];
282 300
 }
283 301
 
@@ -287,13 +305,19 @@ RCT_REMAP_METHOD(request,
287 305
                  rejecter:(RCTPromiseRejectBlock)reject) {
288 306
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
289 307
 
308
+  NSString *randomId = [self insertHandler: handler];
309
+
290 310
   [handler requestWithResolver:^(RNPermissionStatus status) {
291 311
     NSString *strStatus = [self stringForStatus:status];
292 312
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
293 313
     resolve(strStatus);
314
+
315
+    [self.handlers removeObjectForKey:randomId];
294 316
   } rejecter:^(NSError *error) {
295 317
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
296 318
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
319
+
320
+    [self.handlers removeObjectForKey:randomId];
297 321
   }];
298 322
 }
299 323