locationManager:didEnterRegion not called when a beacon is detected

前端 未结 4 422
广开言路
广开言路 2020-12-03 03:45

While testing with beacons (iOS devices) I found the listener beacon giving some unexpected behavior. locationManager:didEnterRegion method is not getting c

4条回答
  •  忘掉有多难
    2020-12-03 04:06

    Check if your methods are implemented in the following way. In viewDidLoad, start moniotoring at the end

    self.beaconRegion.notifyOnEntry=YES;
    self.beaconRegion.notifyOnExit=YES;
    self.beaconRegion.notifyEntryStateOnDisplay=YES;
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
    

    after monitoring start, request state for your defined region

    - (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
    {
        [self.locationManager requestStateForRegion:self.beaconRegion];
    }
    

    after state is determined, start ranging beacons

    -(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
    {
        if (state == CLRegionStateInside)
        {
            //Start Ranging
            [manager startRangingBeaconsInRegion:self.beaconRegion];
        }
        else
        {
            //Stop Ranging here
        }
    }
    

    and implement the following methods according to your needs...

    - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
    {
        self.statusLbl.text=@"Entered region";
    }
    
    -(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
    {
        self.statusLbl.text=@"Exited region";
    }
    
    -(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
    {
        if(beacons.count>0)
        {}
    }
    

    Hope this will solve your problem.

提交回复
热议问题