gpt4 book ai didi

ios - 在 iOS 上获取当前位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:26 25 4
gpt4 key购买 nike

我需要获取当前用户位置(纬度、经度),但我找不到解决方案,因为所有解决方案都只适用于 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];
}

最佳答案

从 iOS 10 开始,您需要将两个键添加到 info.plist 中

NSLocationAlwaysUsageDescription key to your Info.plist file. (Xcode displays this key as "Privacy - Location Always Usage Description" in the Info.plist editor.)

对于 iOS 11

Add the NSLocationWhenInUseUsageDescription key and the NSLocationAlwaysAndWhenInUseUsageDescription key to your Info.plist file. (Xcode displays these keys as "Privacy - Location When In Use Usage Description" and "Privacy - Location Always and When In Use Usage Description" in the Info.plist editor.)

还可以在苹果文档中找到完整的指南:apple corelocation authorization

关于ios - 在 iOS 上获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50152269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com