2014-12-09 179 views
5

我有一个区域监控问题,而应用程序在后台。 如果应用程序处于前台,但进入和退出区域被调用,但不在背景中(有时会触发,但很少)。区域监控信标区域在背景不起作用

信标区域监控如何在iOS 8.1.1上工作?当在烽火台附近时,区域应当立即进入/退出火场?

我应该怎么做才能确保它能正常工作?

Background ModesLocation UpdatesUses Bluetooth LE accessories必须开启背景信标监测工作? GeoFencing在没有这些的情况下为我工作。

我已经做了:

  • 设置这些每一个区域:

    beaconRegion.notifyOnExit=YES; beaconRegion.notifyOnEntry=YES; beaconRegion.notifyEntryStateOnDisplay = YES;

  • 确信一切都在设置是为了(应用程序刷新等)

编辑:

我创建了一个新项目,但它仍然无法工作。下面的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 



_locationManager = [[CLLocationManager alloc] init]; 
_locationManager.pausesLocationUpdatesAutomatically = NO; 
_locationManager.desiredAccuracy = 25; 
_locationManager.delegate = self; 
[_locationManager requestAlwaysAuthorization]; 
[_locationManager startUpdatingLocation]; 

CLBeaconRegion* reg =[self prepareBeacon:@"here i put my UUID" :446 :2196]; 
[_locationManager startMonitoringForRegion:reg]; 
[_locationManager startRangingBeaconsInRegion:reg]; 

return YES; 
} 

-(CLBeaconRegion*)prepareBeacon:(NSString*)uuid :(int)maj :(int)min 
{ 

NSString* identifier = [NSString stringWithFormat:@"%@,%d,%d", uuid, maj, min]; 

CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:uuid] major:maj minor:min identifier:identifier]; 

beaconRegion.notifyOnExit=YES; 
beaconRegion.notifyOnEntry=YES; 
beaconRegion.notifyEntryStateOnDisplay = YES; 

return beaconRegion; 
} 

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

} 

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

} 

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region 
{ 

} 

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status 
{ 

} 

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region 
{ 

} 

一些额外的信息:

  • 除了创造一个新的iOS8上项目,并添加代码我已经添加到NSLocationAlwaysUsageDescription *的.plist文件。
  • 我把断点放在didEnterRegiondidExitRegion。它工作在前台,而在背景(iPhone在主屏幕或锁定)不起作用
  • 测试4S上,的iOS 8.1.1
+1

无需为iBeacons设置背景模式 – 2014-12-12 08:36:19

回答

2

答案是:背景区域的监控是一个谜给我们的用户。有时它会在一秒钟内启动,有时需要更长的时间。这取决于很多因素,但我的主要问题是我使用iPhone 4s。

也许这将帮助任何人,而不会失去这么多的时间:4S吸在后台扫描信标

来源:两个4S手机测试了最新的iOS和iPhone 6 iPhone6获取几秒钟内信通知。

2

你需要确保你已经调用的方法CCLocationManager

- (void) requestAlwaysAuthorization 

这使得你的应用程序大约比低于该行的前景和背景的变化而更新,只允许您的应用程序,而在前台通知:

- (void) requestWhenInUseAuthorization 

一旦用户响应该请求以下方法将更新的授权状态被称为:

- (void) locationManager: (CLLocationManager*) manager 
didChangeAuthorizationStatus: (CLAuthorizationStatus) status 

来源: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/cl/CLLocationManager

+0

这就是我正在使用的。我也有永久验证所需的价值。在* .plist – 2014-12-09 14:33:55

+0

最糟糕的是,有时它有效,有时它不。我已经测试过两款iPhone 4S。 – 2014-12-09 14:54:43

+0

奇怪。让我想想为什么会出现这种情况的另一个原因。 – 2014-12-09 15:25:18