2015-07-13 72 views
2

我正在爲Goefencing進行演示。在iOS 8.4(設備)上未調用CLLocationManager didEnterRegion,didExitRegion和didDetermineState的委託方法

這裏是我的代碼,

- (void) registerRegionForMonitoring { 

    if (self.locationManager == nil) { 
     self.locationManager = [[CLLocationManager alloc] init]; 
     self.locationManager.delegate = self; 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     self.locationManager.distanceFilter = kCLDistanceFilterNone; 

     if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
      [self.locationManager requestAlwaysAuthorization]; 
     } 

     [self.locationManager startUpdatingLocation]; 
    } 

    if(![CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) { 
     [Utilities showAlertWithTitle:@"Geofence" andMessage:@"This app requires region monitoring features which are unavailable on this device."]; 
     return; 
    } 

    for(NSDictionary *dictionary in geofences) { 

     NSString *geofenceId = [NSString stringWithFormat:@"%@", [dictionary valueForKey:@"geofence_id"]]; 
     CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue]; 
     CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue]; 
     CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude); 
     CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue]; 

     CLRegion *geofenceRegion = [[CLCircularRegion alloc] initWithCenter:centerCoordinate radius:regionRadius identifier:geofenceId]; 
     [self.locationManager startMonitoringForRegion:geofenceRegion]; 
    } 
} 

這裏是委託方法

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 

    CLLocation *latestLocation = [locations lastObject]; 
    self.currentLocation = latestLocation; 
} 

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { 
    [Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are in region with identifire %@", region.identifire]]; 
} 

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region { 
    [self.locationManager requestStateForRegion:region]; 
} 

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { 
    [Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are out of region with identifire %@", region.identifire]]; 
} 

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

    if (state == CLRegionStateInside) { 
     // if user already in the region, when monitoring is started 
     [Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are in region with identifire %@", region.identifire]]; 
    } 
} 

我還增加了關鍵"NSLocationAlwaysUsageDescription"plist文件的iOS 8+

也低於增加的背景模式:

  • 應用程序的位置更新註冊使用CoreBluetooth

  • 應用程序共享數據

  • 應用程序通信使用CoreBluetooth

    響應
  • 應用軟件下載的內容推送通知

一切工作適用於iOS 7,也適用於iOS iOS模擬器8.4

它不適用於設備(​​iOS 8.4,iPad和iPod)

我檢查了無線上。在設置>「後臺應用刷新」

我不知道我缺少什麼 -

我也打開「常規」?

歡迎您提出任何建議。提前致謝。

+0

你能解釋一下你在設備上運行時測試應用程序的準確程度嗎?它是通過Xcode模擬的腳步或位置(提示:結果可能不同) –

+0

@AlexPavlov:感謝您的即時響應。我所做的是,我添加了當前位置座標,半徑爲1000米,用於創建區域。然後添加該區域進行監控。所以,它應該調用determineState方法。但事實並非如此。雖然同樣的事情正在我的iOS模擬器8.4上工作 –

回答

0

您是否嘗試過檢查該區域是否真的開始跟蹤?此方法將返回錯誤。

- (void)locationManager:monitoringDidFailForRegion:withError: 

如果你調用start monitoringForRegion你會得到一個很好的位置,從GPS,你可能會得到一個錯誤之前。

另外,您也許想檢查didDetermineState中的狀態CLRegionStateUnknown,因爲這也是一個選項。

相關問題