IOS 8 CLLocationManager 问题(授权不起作用)
Posted
技术标签:
【中文标题】IOS 8 CLLocationManager 问题(授权不起作用)【英文标题】:IOS 8 CLLocationManager Issue (Authorization Not Working) 【发布时间】:2014-08-09 19:22:59 【问题描述】:#import "MyLocationViewController.h"
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
@interface MyLocationViewController ()
@end
@implementation MyLocationViewController
CLLocationManager *locationManager;
- (void)requestAlwaysAuthorization
[locationManager requestAlwaysAuthorization];
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
locationManager = [[CLLocationManager alloc] init];
- (IBAction)unwindToMap:(UIStoryboardSegue *)segue
- (IBAction)getCurrentLocation:(id)sender
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
MKCoordinateRegion mapRegion;
mapRegion.center = mapView.userLocation.coordinate;
mapRegion.span.latitudeDelta = 0.001;
mapRegion.span.longitudeDelta = 0.001;
CLLocationCoordinate2D location = userLocation.coordinate;
float lat = location.latitude;
float lng = location.longitude;
NSDictionary *locationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:lat] , @"Latitude",
[NSNumber numberWithFloat:lng], @"Longitude", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
locationDictionary, @"Location_A",
nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
if (mapView.userLocation != nil)
_longitudeLabel.text = [NSString stringWithFormat:@"%.8f", mapView.userLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:@"%.8f", mapView.userLocation.coordinate.latitude];
[mapView setRegion:mapRegion animated: YES];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@end
这是我的代码。
现在,我知道我需要使用 NSLocationAlwaysUsageDescription 键来编辑我的 info.plist 文件(我这样做了),并且我添加了一个字符串作为描述。
但是,我在实现授权部分时遇到了问题,因为错误仍然是这样的:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
我已经阅读了 CLLocationManager 的 Apple ios 8 文档,但不知何故我的代码无法运行。
有人可以通过查看我需要修改代码中的哪些位置以使其正常工作来帮助我消除上述错误消息吗?
谢谢!
【问题讨论】:
请看这个问题***.com/questions/24874997/… 您实际上在哪里调用视图控制器的requestAlwaysAuthorization
方法(在位置管理器上调用它)?
我在datacalculation.blogspot.in/2014/11/…找到了更多关于位置更新的描述
【参考方案1】:
在用户授权您的应用使用定位服务之前,您不应打开MKMapView
的showsUserLocation
。
您可以实现CLLocationManagerDelegate
的locationManager:didChangeAuthorizationStatus:
方法并在那里打开showsUserLocation
,如下所示:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse)
self.mapView.showsUserLocation = YES;
【讨论】:
我发现了更多描述性信息datacalculation.blogspot.in/2014/11/…以上是关于IOS 8 CLLocationManager 问题(授权不起作用)的主要内容,如果未能解决你的问题,请参考以下文章
CLLocationManager startMonitoringForRegion 在 ios 8.4 上崩溃
CLLocationManager didUpdateLocations 没有被称为 iOS 8 的 plist 条目
CLLocationManager 如何根据 iOS 版本调用正确的委托方法
[iOS][Background][CLLocationManager]地图精度不好
CLLocationManager 未在 iOS 10.2 [Swift] 中调用 didUpdateLocation()