Gen*_*ios 4 objective-c mkmapview cllocationmanager ios
我需要获取当前用户位置(纬度、经度),但我找不到解决方案,因为所有解决方案仅适用于 iOS 6,7,8。例如,我有这段代码,但在 iOS 11.3.1 上它仍然无法工作。
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *latitudeValue;
@property (weak, nonatomic) IBOutlet UILabel *longtitudeValue;
@property (nonatomic,strong) CLLocationManager *locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
} else {
NSLog(@"Location services are not enabled");
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
self.latitudeValue.text = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
self.longtitudeValue.text = [NSString stringWithFormat:@"%f", location.coordinate.longitude];
}
Run Code Online (Sandbox Code Playgroud)
从 iOS 10 开始,您需要将两个键添加到 info.plist 中,如下所示
Info.plist 文件的 NSLocationAlwaysUsageDescription 键。(Xcode 在 Info.plist 编辑器中将此键显示为“隐私 - 位置始终使用说明”。)
对于 iOS 11
将 NSLocationWhenInUseUsageDescription 键和 NSLocationAlwaysAndWhenInUseUsageDescription 键添加到您的 Info.plist 文件中。(Xcode 在 Info.plist 编辑器中将这些键显示为“隐私 - 使用时位置使用说明”和“隐私 - 始终使用位置和使用时使用说明”。)
还可以在苹果文档中找到完整的指南:苹果核心定位授权
| 归档时间: |
|
| 查看次数: |
2191 次 |
| 最近记录: |