Geofencing didEnterRegion,didExitRegion function not Calling in iphone 5S iOS8.1

不打扰是莪最后的温柔 提交于 2020-01-11 09:51:30

问题


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:


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 :)


来源:https://stackoverflow.com/questions/27521220/geofencing-didenterregion-didexitregion-function-not-calling-in-iphone-5s-ios8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!