我已经构建了一个应用,可以在用户退出区域CLCircularRegion
时发送该位置。我正在使用requestWhenInUseauthorization
方法,但是询问用户是否可以使用该位置的提示不会弹出,但该位置会更新。我的位置管理器在继承到NSObject
的自定义类中声明..我在视图控制器类中调用它。
这是我的代码:
class LocationProcessHandler: NSObject, CLLocationManagerDelegate {
static let sharedInstance = LocationProcessHandler()
var locationManager = CLLocationManager()
func startLocationUpdates() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
if #available(iOS 8.0, *) {
// if self.locationManager.respondsToSelector(#selector(locationManager.requestWhenInUseAuthorization)) {
self.locationManager.requestWhenInUseAuthorization()
NSLog("The request when in use authorisation is selected")
self.locationManager.requestAlwaysAuthorization()
}
else {
self.locationManager.startUpdatingLocation()
}
NSLog("Started to monitor the significant location Changes")
self.locationManager.stopMonitoringSignificantLocationChanges()
self.locationManager.startMonitoringSignificantLocationChanges()
// NSLog("the distance filter of the location manager is \(locationManager.distanceFilter)")
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
NSLog("Location Updated")
let persist = Persistence()
let currentLocation : CLLocation = locations[0]
let latitude : Double = currentLocation.coordinate.latitude
let Longitude : Double = currentLocation.coordinate.longitude
let regionID = "GeoFenceTrack"
let region : CLCircularRegion = CLCircularRegion.init(center: CLLocationCoordinate2DMake(latitude, Longitude), radius: Double(persist.getObject(mdmiosagent_Constants.LOCATIONRADIUS))!, identifier: regionID)
NSLog("the center of the region is \(region.center) and the redius of the region is \(region.radius)")
self.sendLocation(currentLocation)
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.delegate = self
self.locationManager.startMonitoringForRegion(region)
}
调用didUpdate方法,但是当应用程序启动或禁用位置服务时,我无法看到任何提示。但是这会在日志中打印
NSLog("The request when in use authorisation is selected")
我无法找出确切原因。
请帮助。 提前致谢。
答案 0 :(得分:1)
在您调用requestWhenInUseAuthorization的部分中,您可以使用以下内容:
$Identity
然后你需要实现 CLLocationManagerDelegate 的委托:
func startLocationUpdates(){
....
//In my case my target is 8+, for this I don't check to prior version
if CLLocationManager.authorizationStatus() == .NotDetermined{
locationManager.requestWhenInUseAuthorization()
}else{
locationManager(locationManager, didChangeAuthorizationStatus: CLLocationManager.authorizationStatus())
}
....
}
您是否也添加了 Info.plist NSLocationWhenInUseUsageDescription?