ios CLLocationManager startUpdatingLocation 不调用 locationManager:didUpdateLocations: 或 locationManager:didFailWithError:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25916841/
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
CLLocationManager startUpdatingLocation not calling locationManager:didUpdateLocations: or locationManager:didFailWithError:
提问by ja.
I'm trying to use the CLLocationManager
framework in my iOS project to access the user's location but when I call
我正在尝试使用CLLocationManager
iOS 项目中的框架来访问用户的位置,但是当我调用
[locationManager startUpdatingLocation]
neither locationManager:didUpdateLocations:
or locationManager:didFailWithError:
are getting called.
既不locationManager:didUpdateLocations:
或locationManager:didFailWithError:
越来越调用。
//myViewController.h
@interface myViewController : UITableViewController <CLLocationManagerDelegate>
@end
//myViewController.m
@implementation myViewController{
CLLocationManager *locationManager;
}
//edit
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
}
//finish edit
-(void)getLocation
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Failed to Get Your Location"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = locations[[locations count] -1];
CLLocation *currentLocation = newLocation;
NSString *longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
NSString *latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
if (currentLocation != nil) {
NSLog(@"latitude: %@", latitude);
NSLog(@"longitude: @"%@", longitude);
}else {
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
}
}
@end
Neither delegate method is being called despite what it says in the documentation:
尽管文档中说了些什么,但这两个委托方法都没有被调用:
"This method returns immediately. Calling this method causes the location manager to obtain an initial location fix (which may take several seconds) and notify your delegate by calling its locationManager:didUpdateLocations:
method [...] In addition to your delegate object implementing the locationManager:didUpdateLocations:
method, it should also implement the locationManager:didFailWithError:
method to respond to potential errors."
“此方法立即返回。调用此方法会导致位置管理器获取初始位置修复(可能需要几秒钟)并通过调用其locationManager:didUpdateLocations:
方法通知您的委托[...] 除了您的委托对象实现该locationManager:didUpdateLocations:
方法之外,它还应该实施locationManager:didFailWithError:
应对潜在错误的方法。”
Don't know how to debug the issue.
不知道如何调试问题。
Thanks,
谢谢,
JA
JA
回答by Mohit Tomar
Just add this in info.plist
只需在 info.plist 中添加这个
NSLocationAlwaysUsageDescription --- I need Location
NSLocationAlwaysUsageDescription --- 我需要位置
NSLocationWhenInUseUsageDescription --- I need Location
NSLocationWhenInUseUsageDescription --- 我需要位置
privacy - location usage description --- I need Location
隐私 - 位置使用说明 --- 我需要位置
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
[locationManager requestWhenInUseAuthorization];
[locationManager startMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
Now it will call your didUpdateToLocation definitely.
现在它肯定会调用你的 didUpdateToLocation。
for more details click here
欲了解更多详情,请点击此处
回答by Emilio Barcelos
Location Services work a bit differently starting in iOS 8.
从 iOS 8 开始,定位服务的工作方式有所不同。
Mainly, you need to add a key NSLocationWhenInUseUsageDescription
to your Info.plistfile, and add a description why your app need Location, such as "Location needed to ...".
主要是你需要在NSLocationWhenInUseUsageDescription
你的Info.plist文件中添加一个键,并添加一个说明为什么你的应用需要位置,比如“位置需要......”。
Note that you might also have to check for iOS version. Only iOS 8 and up have the Location Manager listen to the requestWhenInUseAuthorization
call.
请注意,您可能还需要检查 iOS 版本。仅 iOS 8 及更高版本具有位置管理器监听requestWhenInUseAuthorization
呼叫。
The link below shows more details: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
下面的链接显示了更多详细信息:http: //nevan.net/2014/09/core-location-manager-changes-in-ios-8/
Good luck!
祝你好运!
回答by malex
You strongly need to check that you initialize CLLocationManager on main thread. In other case you will not get updateLocation event.
您强烈需要检查您是否在主线程上初始化了 CLLocationManager。在其他情况下,您将不会收到 updateLocation 事件。
I can't find such info in Apple docs but it works for me anyway.
我在 Apple 文档中找不到这样的信息,但无论如何它对我有用。
回答by orkoden
With iOS 8.0 you need to call -[CLLocationManager requestWhenInUseAuthorization
]
or -[CLLocationManager requestAlwaysAuthorization]
first so the user gets asked to give your app permission to use the location.
在 iOS 8.0 中,您需要先调用-[CLLocationManager requestWhenInUseAuthorization
]
或-[CLLocationManager requestAlwaysAuthorization]
以便用户被要求授予您的应用程序使用该位置的权限。
回答by Bhavin Trivedi
You need to add below things to your project,
您需要将以下内容添加到您的项目中,
In plist of your project add these things:
Key: NSLocationAlwaysUsageDescription Type:String
Key: NSLocationWhenInUseUsageDescription Type:String
In the .m file with
[locationManager startUpdatingLocation]
add this condition :if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) [locationManager requestAlwaysAuthorization];`
在项目的 plist 中添加以下内容:
Key: NSLocationAlwaysUsageDescription Type:String
Key: NSLocationWhenInUseUsageDescription Type:String
在 .m 文件中
[locationManager startUpdatingLocation]
添加以下条件:if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) [locationManager requestAlwaysAuthorization];`
Then only your CLLocationManager
delegate methods will get called.
那么只有你的CLLocationManager
委托方法会被调用。
回答by Dheeraj Kumar
I think you should check this link.You are not retaining the location manager object when declaring.
我认为您应该检查此链接。声明时您没有保留位置管理器对象。
please give property to you object @property(nonatomic, strong)
请给你的对象@property(nonatomic, strong)
Why the CLLocationManager delegate is not getting called in iPhone SDK 4.0?
回答by Jake T.
I had the following in my code base:
我的代码库中有以下内容:
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if( status == kCLAuthorizationStatusAuthorizedAlways ) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 200;
}
}
Turns out that this was getting called in an infinite loop, because initializing a locationManager triggers this method for some reason? I had not realized that. I had put it there to catch when permissions were granted. Instead, I set up a location manager and started updating the location, but then this fired and replaced it with one that wasn't updating the location, and kept looping over and over.
事实证明这是在无限循环中调用的,因为初始化 locationManager 会出于某种原因触发此方法?我没有意识到这一点。我把它放在那里是为了在授予权限时捕捉。相反,我设置了一个位置管理器并开始更新位置,但随后它被触发并替换为一个不更新位置的管理器,并一遍又一遍地循环。
Solution for me was just to add && !_locationManager
to the if condition.
我的解决方案只是添加&& !_locationManager
到 if 条件。
回答by Renetik
So if you are running in simulator don't forget to set location in simulator menu. It looks like if it is set to none nothing is called in delegate... I am not sure than if this can happen on real device too but probably it's simulator specific.
因此,如果您在模拟器中运行,请不要忘记在模拟器菜单中设置位置。看起来如果它被设置为 none 没有任何东西在委托中被调用......我不确定这是否也可能发生在真实设备上,但可能是模拟器特定的。