当前位置:   article > 正文

定位服务在iOS11系统上不能使用的问题_hbulid 在ios上无法启用定位功能

hbulid 在ios上无法启用定位功能

今天我突然发现以前维护的项目中开发的定位功能失效,显示“用户授权失败”。这个功能在当时是能成功实现的。于是我上网查找原因,网上说是因为苹果现在增加了一项新的隐私保护 NSLocationAlwaysAndWhenInUseUsageDescription ,并且原有的 NSLocationAlwaysUsageDescription 被降级为 NSLocationWhenInUseUsageDescription 。 所以在Xcode的控制台输出:

This app has attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain both NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescription keys with string values explaining to the user how the app uses this data

 

依照上述要求,我在项目的info.plist文件中加入

  1. <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
  2.     <string>是否允许开启定位以始终访问位置</string>
  3. <key>NSLocationWhenInUseUsageDescription</key>
  4.     <string>是否允许开启定位以在使用期间访问位置</string>

 

然后又重新实现了定位

  1. /*
  2. iOS8以前:系统会自动申请用户位置权限
  3. iOS8之后(包含iOS8.0): 必须要程序员去申请用户的位置权限
  4. */
  5. #import "ViewController.h"
  6. #import <CoreLocation/CoreLocation.h>
  7. @interface ViewController ()<CLLocationManagerDelegate>
  8. @property (nonatomic,strong) CLLocationManager *locationManager;
  9. @end
  10. @implementation ViewController
  11. - (void)viewDidLoad {
  12. [super viewDidLoad];
  13. //位置管理器常见属性
  14. //当用户移动了多长的距离之后,才更新用户位置,单位(米)
  15. self.locationManager.distanceFilter = 10;
  16. //定位精度,精度越高越费电
  17. self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
  18. //extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation; 导航使用最好进度
  19. //extern const CLLocationAccuracy kCLLocationAccuracyBest;  最高精度
  20. //extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;  10M
  21. //extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters; 100M
  22. //extern const CLLocationAccuracy kCLLocationAccuracyKilometer; 1000M
  23. //extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers; 3000M
  24. self.locationManager.activityType = CLActivityTypeFitness;
  25. //CLActivityTypeOther = 1,
  26. //CLActivityTypeAutomotiveNavigation,    // 汽车导航
  27. //CLActivityTypeFitness,                // 步行导航
  28. //CLActivityTypeOtherNavigation         // 其他导航,比如轮船,火车,飞机
  29. if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
  30. //申请用户位置权限
  31. //无论应用在前台还是在后台都需要使用用户的位置信息
  32. [self.locationManager requestAlwaysAuthorization];
  33. }else{
  34. //开始更新用户位置
  35. [self.locationManager startUpdatingLocation];
  36. }
  37. }
  38. #pragma mark - CLLocationManagerDelegate
  39. //当授权状态发生改变了就调用该代理方法
  40. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
  41. //当前请求到用户的权限后开始定位
  42. if (status == kCLAuthorizationStatusAuthorizedAlways) {
  43. [self.locationManager startUpdatingLocation];
  44. }else{
  45. NSLog(@"用户授权失败");
  46. }
  47. }
  48. //当更新用户位置的时候执行
  49. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
  50. //距离当前时间最近的那个位置是这个数组的最后一个元素
  51. CLLocation *location = locations.lastObject;
  52. CLLocationCoordinate2D coordinate = location.coordinate;
  53. NSLog(@"当前纬度:%f 当前经度:%f", coordinate.latitude, coordinate.longitude);
  54. //根据经纬度反向地理编译出地址信息
  55. CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
  56. [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
  57. for (CLPlacemark *placemark in placemarks) {
  58. NSDictionary *address = [placemark addressDictionary];
  59. NSLog(@"当前地址:%@ %@ %@ %@ %@",address[@"Country"],address[@"City"],address[@"SubLocality"],address[@"Street"],address[@"Name"]);
  60. }
  61. }];
  62. }
  63. //懒加载
  64. - (CLLocationManager *)locationManager{
  65. if (_locationManager == nil) {
  66. _locationManager = [[CLLocationManager alloc] init];
  67. _locationManager.delegate = self;
  68. }
  69. return _locationManager;
  70. }
  71. @end

Xcode控制台输出

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/525860
推荐阅读
相关标签
  

闽ICP备14008679号