【问题标题】:Not Locating user position with MapView Kit未使用 MapView 工具包定位用户位置
【发布时间】:2015-02-12 16:08:21
【问题描述】:

当应用程序运行时它给了我这个:

尝试在不提示位置授权的情况下启动 MapKit 位置更新。必须先调用 -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization]。

我知道这是因为我们必须首先要求用户使用他的位置,我按照他们的建议做了:

<key>NSLocationAlwaysUsageDescription</key>
<string>Message</string>

  or/and

<key>NSLocationWhenInUseUsageDescription</key>
<string>Message</string>

整合 Info.plist 文件

这是它的外观:

但它给了我这个消息,并且在 iOS 模拟器或设备 (iPad) 中都找不到设备。

【问题讨论】:

标签: ios objective-c mapkit core-location


【解决方案1】:

试试这个:

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

在你的课程实施之前

还有这个:

#ifdef __IPHONE_8_0
    if(IS_OS_8_OR_LATER) {
        [self.locationManager requestWhenInUseAuthorization];
    }
#endif
    [self.locationManager startUpdatingLocation];

或:

#ifdef __IPHONE_8_0
    if(IS_OS_8_OR_LATER) {
        [self.locationManager requestAlwaysAuthorization];
    }
#endif
    [self.locationManager startUpdatingLocation];

不是两者。 对我来说,我在 viewDidLoad 方法中这样做

会没事的;p

【讨论】:

  • 不要使用 IS_OS_8_OR_LATER。有适当的方法来检查 API 是否存在。
【解决方案2】:

在 iOS 8 上执行此授权

    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
        [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse
        //[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways
        ) {
        // Will open an confirm dialog to get user's approval
        [locationManager requestWhenInUseAuthorization];
        [locationManager requestAlwaysAuthorization];
        //[_locationManager requestAlwaysAuthorization];
    } else {
        [locationManager startUpdatingLocation]; //Will update location immediately
    }

【讨论】:

  • 还是给我那个信息
  • 进入项目设置 - 功能 - 后台模式 - 选择位置更新。然后再试一次。
  • 不要检查systemVersion。有适当的方法来检查 API 是否存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多