Objective c 当前位置返回0
我正在开发的Xcode应用程序需要获取用户iOS设备的纬度和经度。尽管目前我得到的两个值都是0。它不要求获得位置许可,我使用的是真实的设备,而不是模拟器Objective c 当前位置返回0,objective-c,core-location,Objective C,Core Location,我正在开发的Xcode应用程序需要获取用户iOS设备的纬度和经度。尽管目前我得到的两个值都是0。它不要求获得位置许可,我使用的是真实的设备,而不是模拟器 NSLocationAlwaysUsageDescription在我的info.plist中 我还在.h文件中导入了CoreLocation和 @interface LocationViewController : UIViewController <CLLocationManagerDelegate> 在startUpdatin
NSLocationAlwaysUsageDescription
在我的info.plist中
我还在.h文件中导入了CoreLocation和
@interface LocationViewController : UIViewController <CLLocationManagerDelegate>
在
startUpdatingLocation
#ifdef __IPHONE_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];
}
#endif
[self.locationManager startUpdatingLocation];
同时调用[self getCurrentLocation]代码>在视图中加载&视图显示
两者
它会起作用的
另外,请确保在info.plist文件中也有requestwhenuseauthorization
的条目
请参阅此。希望,这有帮助
进行以下更改:
-(CLLocationCoordinate2D) getLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager requestWhenInUseAuthorization];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSLog(@"Status : %d", status);
}
转到设置>隐私>位置>应用程序>始终
并查看“状态”值如何变化。哪个iOS?我相信你正在测试iOS 8及以上版本。。。是吗?是的,iOS 8.2,谢谢:)尝试了它,但在if语句中出错。我注释掉了if语句并保留了[self.locationManager requestwhenUseAuthorization];但仍然返回0。正如你在viewdidload中所说的那样/viewdidappear@DanWinter-维杰斯:看看我在做什么。。它会工作的…完美的工作!如何让应用程序请求位置权限,以便我不必在“设置”中更改它?请在info.plist中添加“NSLocationAlwaysUsageDescription”。
#ifdef __IPHONE_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];
}
#endif
[self.locationManager startUpdatingLocation];
-(CLLocationCoordinate2D) getLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager requestWhenInUseAuthorization];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSLog(@"Status : %d", status);
}