Skip to content

Commit

Permalink
Merge pull request #8 from aYukoIzuhara/support-when-in-use
Browse files Browse the repository at this point in the history
Support kCLAuthorizationStatusAuthorizedWhenInUse
  • Loading branch information
aYukoIzuhara authored Apr 5, 2018
2 parents 6976429 + ab42669 commit 55dc05c
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions iOS/ABFBeacon/ABFBeacon.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@interface ABFBeacon ()
@property (nonatomic, strong) CBPeripheralManager *peripheralManager;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic) CLAuthorizationStatus authStatus;
@end

@implementation ABFBeacon
Expand Down Expand Up @@ -67,6 +68,8 @@ - (instancetype)initSharedInstance
name:UIApplicationDidBecomeActiveNotification
object:nil];
*/

_authStatus = [CLLocationManager authorizationStatus];
}
return self;
}
Expand Down Expand Up @@ -140,11 +143,21 @@ - (BOOL)isMonitoringCapable

- (void)updateMonitoring
{
CLAuthorizationStatus newStatus = [CLLocationManager authorizationStatus];

if ([self isMonitoringCapable]) {
if ((self.authStatus == kCLAuthorizationStatusAuthorizedAlways
&& newStatus == kCLAuthorizationStatusAuthorizedWhenInUse)
|| (self.authStatus = kCLAuthorizationStatusAuthorizedWhenInUse
&& newStatus == kCLAuthorizationStatusAuthorizedAlways)) {
[self disableMonitoring];
}
[self enableMonitoring];
} else {
[self disableMonitoring];
}

self.authStatus = newStatus;
}

- (void)enableMonitoring
Expand Down Expand Up @@ -178,7 +191,14 @@ - (void)enableMonitoringForRegion:(ABFBeaconRegion *)region
return;
}

[_locationManager startMonitoringForRegion:region];
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
if (region.rangingEnabled) {
[self startRanging:region];
}
} else {
[_locationManager startMonitoringForRegion:region];
}
region.isMonitoring = YES;
}

Expand Down Expand Up @@ -452,8 +472,10 @@ - (NSString *)locationAuthorizationStatusString:(CLAuthorizationStatus)status
return @"Restricted";
case kCLAuthorizationStatusDenied:
return @"Denied";
case kCLAuthorizationStatusAuthorized:
return @"Authorized";
case kCLAuthorizationStatusAuthorizedAlways:
return @"AuthorizedAlways";
case kCLAuthorizationStatusAuthorizedWhenInUse:
return @"AuthorizedWhenInUse";
}
return @"";
}
Expand Down

0 comments on commit 55dc05c

Please sign in to comment.