Geofencing didEnterRegion,didExitRegion函数没有在iphone 5S iOS8.1中调用

时间:2014-12-17 08:31:35

标签: ios objective-c cllocationmanager

我整天都进行了调试,并且委托完全被调用了。

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

这里是我的标准代码,需要监视器。使用CoreLocation.framework。

[locationManager startMonitoringForRegion:geofence];

并在我的plist中注册了这些。

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

后台应用刷新启用,但我没有看到我的应用。

我曾尝试在LocationManager的实例中打印我的monitoredRegions,并且有我的受监控区域。

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

和regionMonitoringAvailable为真。

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

在iOS 8中,我曾要求requestAlwaysAuthorization

[locationManager requestAlwaysAuthorization];

我曾尝试过三种状态,app在前台,应用程序在后台,app不活跃。这些州中没有一个叫做。

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

根本没有任何错误。

我曾尝试过

[locationManager requestStateForRegion:geofence];

工作正常。

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

在我的requestStateForRegion中触发。

我不知道是什么让didEnterRegion没有被调用,我知道在iOS 7及更高版本的设备上工作但我现在没有这样的设备可以作证。

也许requestStateForRegion可以满足我的要求,但我仍然无法弄清楚DidEnterRegion是如何工作的。并且这些都不会触发任何错误消息来告诉开发人员调试。

1 个答案:

答案 0 :(得分:2)

我面临同样的问题,以下是我遵循的步骤并取得成功。

  • locationmanager中添加地理围栏数组数据后。使用以下代码。

    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."];
     }
    
  • 确保您的无线网络已开启。
  • 使用以下代表检查区域的开始监控。

    -(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
    {
        NSLog(@"Started monitoring %@ region", region.identifier);
    }
    
  • 将这两种委托方法用于地理围栏。 1)DidEnter 2)DidExit

  • 使用某些移动或其他位置测试您的设备(确保将地理围栏设置为100米半径的不同位置)。同样,您在DidEnterDidExit方法中实现本地通知,因此无需调试。一旦你的方法将被调用,那时本地通知就会消失。
  • 获得成功:)