| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- //
- // RNPLocation.m
- // ReactNativePermissions
- //
- // Created by Yonah Forst on 11/07/16.
- // Copyright © 2016 Yonah Forst. All rights reserved.
- //
- #import "RNPLocation.h"
- #import <CoreLocation/CoreLocation.h>
- @interface RNPLocation() <CLLocationManagerDelegate>
- @property (strong, nonatomic) CLLocationManager* locationManager;
- @property (copy) void (^completionHandler)(NSString *);
- @end
- @implementation RNPLocation
- + (NSString *)getStatusForType:(NSString *)type
- {
- int status = [CLLocationManager authorizationStatus];
- switch (status) {
- case kCLAuthorizationStatusAuthorizedAlways:
- return RNPStatusAuthorized;
- case kCLAuthorizationStatusAuthorizedWhenInUse:
- return [type isEqualToString:@"always"] ? RNPStatusDenied : RNPStatusAuthorized;
- case kCLAuthorizationStatusDenied:
- return RNPStatusDenied;
- case kCLAuthorizationStatusRestricted:
- return RNPStatusRestricted;
- default:
- return RNPStatusUndetermined;
- }
- }
- - (void)request:(NSString*)type completionHandler:(void (^)(NSString *))completionHandler
- {
- NSString *status = [RNPLocation getStatusForType:nil];
- if (status == RNPStatusUndetermined) {
- self.completionHandler = completionHandler;
- if (self.locationManager == nil) {
- self.locationManager = [[CLLocationManager alloc] init];
- self.locationManager.delegate = self;
- }
- [self.locationManager requestWhenInUseAuthorization];
- } else {
- if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse && [type isEqualToString:@"always"]) {
- completionHandler(RNPStatusDenied);
- } else {
- completionHandler(status);
- }
- }
- }
- -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
- if (status != kCLAuthorizationStatusNotDetermined) {
- if (self.locationManager) {
- self.locationManager.delegate = nil;
- self.locationManager = nil;
- }
- if (self.completionHandler) {
- //for some reason, checking permission right away returns denied. need to wait a tiny bit
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
- self.completionHandler([RNPLocation getStatusForType:nil]);
- self.completionHandler = nil;
- });
- }
- }
- }
- @end
|