我正尝试在我的控制台上执行NSLog坐标,但它不起作用。
我在标题中链接了核心位置
@implementation ViewController {
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
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 didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSLog(@"%f", currentLocation.coordinate.longitude);
NSLog(@"%f", currentLocation.coordinate.latitude);
}
}但是我在控制台上没有得到任何东西,有人知道可能会出什么问题吗?
发布于 2014-10-20 01:14:02
从iOS 8开始,在开始更新位置之前,你必须征得用户的许可。在此之前,您必须添加用户将在权限请求中收到的消息。在您的.plist文件中添加这两个键(如果您想使用两种类型的位置获取),并用您自己的消息填充它们:NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription

然后请求许可,就在启动CLLocationManager之前
[self.locationManager requestWhenInUseAuthorization];或/和
[self.locationManager requestAlwaysAuthorization];为了避免在iOS 7及更低版本上崩溃,您可以定义一个宏来检查操作系统版本:
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)然后你可以这样做:
if(IS_OS_8_OR_LATER) {
// Use one or the other, not both. Depending on what you put in info.plist
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}现在它应该可以工作了。
宏源:
https://stackoverflow.com/questions/26452785
复制相似问题