2014-12-09 61 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