xcode didUpdateLocations 没有被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26178483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
didUpdateLocations not being called
提问by Andrew
I have my object set as the locationManager's delegate, and the didChangeAuthorizationStatus method is called, which does this:
我将我的对象设置为 locationManager 的委托,并调用 didChangeAuthorizationStatus 方法,它执行以下操作:
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.AuthorizedWhenInUse {
self.locationManager.startUpdatingLocation()
}
}
I also have this method, which never gets called following this:
我也有这个方法,它永远不会在下面被调用:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if locations.count == 0 {
return
}
//Do stuff
}
Any thoughts as to why this might not be being called? I suppose the object being deallocated is an option, but then it'd also be deallocated by the time it hit the authorizationStatus method.
关于为什么可能不会被调用的任何想法?我想被释放的对象是一个选项,但是它也会在它遇到 authorizationStatus 方法时被释放。
回答by timgcarlson
This is way late for an answer, but I stumbled across this when I was having the same issue (didChangeAuthorizationStatus
was being called but not didUpdateLocations
). Well of course it's something not code related, but rather I was testing in the simulator which did not have a location set. Thus the location was never found, resulting in didUpdateLocations
never being called.
这对答案来说太晚了,但是当我遇到同样的问题(didChangeAuthorizationStatus
被调用但没有被调用didUpdateLocations
)时,我偶然发现了这个。当然,这与代码无关,而是我在没有设置位置的模拟器中进行测试。因此从未找到该位置,导致didUpdateLocations
从未被调用。
To fix this... in the simulator go to Debug -> Location-> <choose location>
.
要解决此问题...在模拟器中转到Debug -> Location-> <choose location>
.
EDITIt should also be noted that locationManager:didFailWithError:
will run if the location is not set in the simulator, as you'd expect.
编辑还应该注意,locationManager:didFailWithError:
如果未在模拟器中设置位置,它将运行,如您所料。
回答by ios_dev
It's working in iOS 10and iOS 11as well,
它也适用于iOS 10和iOS 11,
Create propertyof CLLocationManagerusing strongand add CLLocationManagerDelegate
建立财产的CLLocationManager使用强并添加CLLocationManagerDelegate
@property (strong, nonatomic) CLLocationManager *locationManager;
Add below properties into your info.plistfile
将以下属性添加到您的info.plist文件中
Privacy - Location When In Use Usage Description
Privacy - Location Always Usage Description
Privacy - Location Usage Description
Add below method in to your .m file
将以下方法添加到您的.m 文件中
-(void)getCurrentLocation
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
self.locationManager.distanceFilter=kCLDistanceFilterNone;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
}
and just call[self getCurrentLocation]
method.
只需调用[self getCurrentLocation]
方法。
回答by zisoft
Here is a snippet from my app:
这是我的应用程序的一个片段:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
...
locationManager.delegate = self
locationManager.activityType = CLActivityType.Fitness
locationManager.distanceFilter = 10 // 10m
locationManager.requestAlwaysAuthorization()
// get current location
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .Authorized {
locationManager.startUpdatingLocation()
}
}
You have to put the NSLocationAlwaysUsageDescription
key in your Info.plist
and set the value to the authorization question. This is a requirement as of iOS 8.
您必须将NSLocationAlwaysUsageDescription
密钥放入您的密钥Info.plist
并将值设置为授权问题。这是 iOS 8 的要求。