2015-10-22 18 views
2

當我開始監視區域像RequestStateForRegion斯威夫特

locationManager.startMonitoringForRegion(tGeoFence[wert][wert2]) 

,並試圖確定它是否之後,像這樣已經進入:

for region in locationManager.monitoredRegions { 
    if let cireg = region as? CLCircularRegion { 
     if cireg.identifier == tGeoFence[wert][wert2].identifier { 
      locationManager.requestStateForRegion(cireg) 
     } 
    } 
} 

不起作用,原因當執行代碼的第二部分時,該區域的註冊未完成。延遲執行該部分似乎很難(diddeterminestate not always called),有沒有更好的方法來解決這個問題?

回答

4

發現了,顯然didStartMonitoringForRegion委託功能是要求requestStateForRegion正確的地方:

func locationManager(manager: CLLocationManager, didStartMonitoringForRegion region: CLRegion) { 
    locationManager.requestStateForRegion(region) 
} 

編輯:可怕的事情是,我還需要一個短暫的延遲,有時requestStateForRegion不叫:

func locationManager(manager: CLLocationManager, didStartMonitoringForRegion region: CLRegion) { 
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { 
     self.locationManager.requestStateForRegion(region) 
    } 
} 

:(