Ios CLLocation当前位置不工作
我的类中有此代码Ios CLLocation当前位置不工作,ios,objective-c,xcode,core-location,Ios,Objective C,Xcode,Core Location,我的类中有此代码ViewController: CLLocationManager *locationManager; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. locationManager = [[CLLocationManager alloc] init]; [loc
ViewController
:
CLLocationManager *locationManager;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
[locationManager requestWhenInUseAuthorization];
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Did finish with error - %@", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"did Update Location - %@", newLocation);
CLLocation *currentLocation = newLocation;
if(currentLocation != nil) {
_longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}
但我没有得到允许访问的位置或弹出窗口
我正在使用核心位置框架
点击按钮,我正在打印标签中的纬度、经度和地址
我正在模拟器上测试此代码。在viewDidLoad中尝试此代码
//---- For getting current gps location
locationManager = [CLLocationManager new];
locationManager.delegate = self;
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
//------
您还必须为NSLocationAlwaysUsageDescription或nsLocationWhenUsageDescription键添加字符串到应用程序的Info.plist。在viewDidLoad中尝试此代码
//---- For getting current gps location
locationManager = [CLLocationManager new];
locationManager.delegate = self;
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
//------
您还必须为NSLocationAlwaysUsageDescription或nsLocationWhenUsageDescription键添加一个字符串到应用程序的Info.plist。有时模拟器无法与支持位置的服务配合使用
苹果设备进行完美测试
在info.plist
文件中添加以下键以获得允许访问弹出窗口
或者将它们添加为更新info.plist
文件源代码
<key>NSLocationAlwaysUsageDescription</key>
<string>message for location uses</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>message for location uses</string>
NSLocationAlwaysUsageDescription
位置使用信息
NSLocationWhenUse用途说明
位置使用信息
有时模拟器无法与支持位置的服务配合使用苹果设备进行完美测试
在info.plist
文件中添加以下键以获得允许访问弹出窗口
或者将它们添加为更新info.plist
文件源代码
<key>NSLocationAlwaysUsageDescription</key>
<string>message for location uses</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>message for location uses</string>
NSLocationAlwaysUsageDescription
位置使用信息
NSLocationWhenUse用途说明
位置使用信息
您可以在plist中添加:
<key>NSLocationAlwaysUsageDescription</key>
<key>NSLocationWhenInUseUsageDescription</key>
NSLocationAlwaysUsageDescription
NSLocationWhenUse用途说明
您可以在plist中添加:
<key>NSLocationAlwaysUsageDescription</key>
<key>NSLocationWhenInUseUsageDescription</key>
NSLocationAlwaysUsageDescription
NSLocationWhenUse用途说明
我提醒您在plist中添加授权请求,并使用您可以获得的设备进行检查。您是否在.plist中添加了授权请求说明?请检查上面的KAR check链接,它为您提供了我在info plist文件中添加的解决方案。感谢@user3182143我提醒您在plist中添加授权请求,并使用您可以获得的设备进行检查。您是否在.plist中添加了授权请求说明?请检查上面的KAR check链接,它为您提供了在info plist文件中添加的解决方案。谢谢@user3182143