iOS startUpdatingLocation 从未调用过
Posted
技术标签:
【中文标题】iOS startUpdatingLocation 从未调用过【英文标题】:iOS startUpdatingLocation never called 【发布时间】:2014-09-24 12:22:43 【问题描述】:我一直在尝试获取位置坐标。在我打电话给[locationManager startUpdatingLocation];
之后,什么也没有发生。
它从不调用- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
有什么想法吗?
注意:我已经设置了CLLocationManagerDelegate
并将我的CLLocationManager
设置为strong
类型。在我的viewDidLoad
中,我设置了我的代表,我正在尝试使用requestAlwaysAuthorization
。
ViewController.m
- (void)viewDidLoad
[super viewDidLoad];
NSLog(@"locationServicesEnabled: %@", [CLLocationManager locationServicesEnabled] ? @"YES":@"NO");
NSLog(@"locationManager init");
NSLog(@"geocoder init");
locationManager = [[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
//locationManager.delegate = self;
[locationManager setDelegate:self];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestAlwaysAuthorization];
NSLog(@"locationManager startUpdatingLocation");
[locationManager startUpdatingLocation];
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
NSLog(@"locationManager:manager");
NSLog(@"didFailWithError: %@", 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;
NSLog(@"locations %@", locations);
CLLocation *newLocation = [locations lastObject];
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil)
longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
// Stop Location Manager
[locationManager stopUpdatingLocation];
// Reverse Geocoding
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0)
placemark = [placemarks lastObject];
locationLabel.text = [NSString stringWithFormat:@"%@, %@",
placemark.locality,
placemark.administrativeArea];
else
NSLog(@"%@", error.debugDescription);
];
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLGeocoder *geocoder;
@property (strong, nonatomic) CLPlacemark *placemark;
日志
2014-09-24 08:18:39.783 appName[1201:130186] locationServicesEnabled: YES
2014-09-24 08:18:39.784 appName[1201:130186] locationManager init
2014-09-24 08:18:39.784 appName[1201:130186] geocoder init
2014-09-24 08:18:39.785 appName[1201:130186] locationManager startUpdatingLocation
我也试过删除[locationManager stopUpdatingLocation];
,但它仍然没有做任何事情。
其他说明:如果我检查设备设置/隐私,则表明我的应用确实具有位置权限。我所有的.h
属性在.m
文件中都是@synthesize
。
【问题讨论】:
【参考方案1】:如果您使用的是 ios 8,则需要调用以下任一方法:
-[CLLocationManager requestWhenInUseAuthorization]
或 -[CLLocationManager requestAlwaysAuthorization]
先调用[locationManager startUpdatingLocation];
。
您还需要在 .plist 中为这些键 NSLocationUsageDescription
、NSLocationWhenInUseUsageDescription
和 NSLocationAlwaysUsageDescription
提供描述
【讨论】:
我有[locationManager requestAlwaysAuthorization];
我的问题是我需要将NSLocationAlwaysUsageDescription
添加到.plist
。我现在正在获取位置数据。 (虽然数据不正确,但我怀疑这是模拟器的问题。)非常感谢。
位置管理器在当前 (Xcode 6.0.1) 模拟器中非常糟糕。以上是关于iOS startUpdatingLocation 从未调用过的主要内容,如果未能解决你的问题,请参考以下文章
同时使用 startUpdatingLocation 和 startMonitoringSignificantChanges?
startUpdatingLocation 不再在 XCode 6.1 中工作
StartUpdatingLocation 与重大变化的位置服务
startUpdatingLocation 仅在 wifi 开启时有效,但仅在 3G 开启时无效
找不到 CLLocationManager startUpdatingLocation
CLLocationManager startUpdatingLocation 与 startMonitoringSignificantLocationChanges