I want call ‘startMonitoringForRegion’ in method ‘didChangeAuthorizationStatus’ and everything works fine, basically because code is copied from Estimote Example. This code is in ViewController. Issue occurs when I’m using similar code but placed in separated controller.
This code works fine and method ‘didChangeAuthorizationStatus’ also ‘didStartMonitoringForRegion’ is called every time so I open app or changeStatus. So success.
ViewController.m
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.region = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"myUUID"]
identifier:@"EstimoteSampleRegion"];
self.beaconManager = [[ESTBeaconManager alloc] init];
self.beaconManager.delegate = self;
[self startRangingBeacons];
}
-(void)startRangingBeacons
{
if ([ESTBeaconManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
{
[self.beaconManager requestAlwaysAuthorization];
}
else if([ESTBeaconManager authorizationStatus] == kCLAuthorizationStatusDenied)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Access Denied"
message:@"You have denied access to location services. Change this in app settings."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
else if([ESTBeaconManager authorizationStatus] == kCLAuthorizationStatusRestricted)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Not Available"
message:@"You have no access to location services."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
}
- (void)beaconManager:(id)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (status != kCLAuthorizationStatusNotDetermined && status != kCLAuthorizationStatusDenied )
{
[self.beaconManager startMonitoringForRegion:self.region];
}
}
- (void)beaconManager:(id)manager didStartMonitoringForRegion:(CLBeaconRegion *)region{
NSLog(@"didStartMonitoringForRegion %@",region);
}
This code works partially. Method ‘requestAlwaysAuthorization’ in ‘BeaconManager’ is calling once but then ‘didChangeAuthorizationStatus’ is not even when I’ve manually called ‘startMonitoringForRegion’ then method ‘didStartMonitoringForRegion’ is not called.
Separeted BeaconManager
ViewController.m
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.region = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"myUUID"]
identifier:@"EstimoteSampleRegion"];
self.beaconManager = [[BeaconManager alloc]initWithRegion:self.region];
[self.beaconManager startMonitoring];
}
BeaconManager.swift
class BeaconManager: NSObject,ESTBeaconManagerDelegate {
private lazy var secureBeaconManager:ESTSecureBeaconManager = {
let beaconManagerForReturn = ESTSecureBeaconManager()
beaconManagerForReturn.delegate = self
return beaconManagerForReturn
}()
private lazy var beaconsArray = [CLBeacon]()
var region:CLBeaconRegion!
var delegate:BeaconDelegate?
init(region:CLBeaconRegion) {
super.init()
self.region = region
}
/**
Method which check AuthorizationStatus for application, and will start monitoring if status is equal .AuthorizedAlways
*/
func startMonitoring() {
setupLocalizationAuthorization()
}
private func setupLocalizationAuthorization(){
let status = ESTSecureBeaconManager.authorizationStatus()
if (status == .NotDetermined){
secureBeaconManager.requestAlwaysAuthorization()
}
if (status == .Denied){
delegate?.beaconManager(self, locationAutorizationFail: .Denied)
}
if (status == .Restricted){
delegate?.beaconManager(self, locationAutorizationFail: .Restricted)
}
}
// MARK: - BeaconManager
func beaconManager(manager: AnyObject, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if (status != .NotDetermined && status != .Denied){
secureBeaconManager.startMonitoringForRegion(region)
}
}
func beaconManager(manager: AnyObject, didStartMonitoringForRegion region: CLBeaconRegion) {
print("didStartMonitoring for \(region)")
}
}
Question
Why this is happening? It’s thread issue? I have no clue what’s going wrong here. From I can see both codes are similar but instructions are placed in different place. It’s no matter If I use Swift or Obj-c (tried it already).