CLLocationManager startUpdatingLocation不调用locationManager:didUpdateLocations:或locationManager:didFailWithError: [英] CLLocationManager startUpdatingLocation not calling locationManager:didUpdateLocations: or locationManager:didFailWithError:
问题描述
我正在尝试在我的iOS项目中使用 CLLocationManager
框架来访问用户的位置,但是当我打电话时
I'm trying to use the CLLocationManager
framework in my iOS project to access the user's location but when I call
[locationManager startUpdatingLocation]
既不 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:
此方法立即返回。调用此方法会导致位置管理器获取初始位置修复(可能需要几秒钟)并通过调用其 locationManager来通知您的代理:didUpdateLocations:
方法[...]除了实现 locationManager:didUpdateLocations:
方法的委托对象之外,它还应该实现 locationManager:didFailWithError:
方法回应潜在的错误。
"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."
不知道如何调试问题。
谢谢,
JA
推荐答案
只需在info.plist中添加这个
Just add this in info.plist
NSLocationAlwaysUsageDescription ---我需要位置
NSLocationAlwaysUsageDescription --- I need Location
NSLocationWhenInUseUsageDescription ---我需要位置
NSLocationWhenInUseUsageDescription --- I need Location
隐私 - 位置使用说明---我需要位置
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];
现在它肯定会调用你的didUpdateToLocation。
Now it will call your didUpdateToLocation definitely.
了解更多详情点击这里
这篇关于CLLocationManager startUpdatingLocation不调用locationManager:didUpdateLocations:或locationManager:didFailWithError:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!