Geofencing didEnterRegion,didExitRegion function n

2020-04-12 07:06发布

I had debugged all day and delegate did get called at all.

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

Well here is my standard code calling for a monitor. use CoreLocation.framework.

[locationManager startMonitoringForRegion:geofence];

And registered these in my plist.

<key>NSLocationAlwaysUsageDescription</key>
<string>Lugang</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Lugang</string>

background app refresh in enable, but I didn't see my app within.

I had tried to print my monitoredRegions in instance of LocationManager and there is my monitored region.

NSLog(@"%@" ,locationManager.monitoredRegions);

and regionMonitoringAvailable is true.

NSLog(@"%d" , [CLLocationManager regionMonitoringAvailable] );

In iOS 8, I had asked for requestAlwaysAuthorization

[locationManager requestAlwaysAuthorization];

I had tried three states , app in foreground, app in background, app is not active. none of these states called.

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

didn't get any errors at all.

And I had tried

[locationManager requestStateForRegion:geofence];

works fine.

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region

did trigger in my requestStateForRegion.

I don't know what it is to make didEnterRegion didn't get called and I know in iOS 7 and above devices work but I don't have such a device to testify now.

Maybe requestStateForRegion could fulfill my requestment, But I still couldn't figure out how does DidEnterRegion not work. And none of these trigger any error messages to tell developers to debug.

1条回答
▲ chillily
2楼-- · 2020-04-12 07:42

I have face same problem, Following are the steps I was follow and get succeed.

  • After add your geofence Array data in locationmanager. Use below code for the same.

    for (CLRegion *monitored in [locationManagerGeofence monitoredRegions])
     {
         [locationManagerGeofence stopMonitoringForRegion:monitored];
     }
     self.geofencesArray = [NSMutableArray arrayWithArray:[self buildGeofenceData]];
     if([CLLocationManager regionMonitoringAvailable])
     {
         for (CLRegion *region in self.geofencesArray)
         {
             [locationManagerGeofence startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
         }
     }
     else
     {
         [BB_Global displayAlertWithTitle:@"" message:@"This app requires region monitoring features which are unavailable on this device."];
     }
    
  • Make sure your wifi is On.
  • Check your start monitoring for region with below delegate.

    -(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
    {
        NSLog(@"Started monitoring %@ region", region.identifier);
    }
    
  • Use these two delegate method for geofence. 1) DidEnter 2)DidExit

  • Test your device with some movement or some other place (Make sure set your geofence set with different location with 100 meter radius). for the same you implement local notification in DidEnter and DidExit method so no need to debug. once your method will be call that time local notification is fire.
  • Get Success :)
查看更多
登录 后发表回答