【问题标题】:iOS 8 Location Service not working in spite of adding NSLocationWhenInUseUsageDescription尽管添加了 NSLocationWhenInUseUsageDescription,但 iOS 8 定位服务仍无法正常工作
【发布时间】:2014-12-18 01:58:32
【问题描述】:

我正在使用iOS8 [8.0.2],无论我尝试什么,我都无法让CLLocationManager 工作。

我在info.plist 中添加了以下几行。

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app use location services.</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>This app use location services.</string>

在我的实现文件中:

In ViewDidLoad : 

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];

但没有调用以下方法。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

我错过了什么?

【问题讨论】:

    标签: ios ios8 mkmapview cllocationmanager


    【解决方案1】:

    检查这个post

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    

    上述委托在 iOS 6 中已弃用。改用以下委托:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    

    编辑

    在通过locationManager:didUpdateLocations:获得授权之前不要将mapView.showUserLocation设置为YES

    这样设置:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        self.mapView.showUserLocation = YES;
        [locationManager stopUpdatingLocation];
    }
    

    【讨论】:

    • 您是否在项目中添加了 Core Location 框架?
    • 是的.. 我做到了。我删除了该应用程序,当我运行时,它给了我这个警告:尝试启动 MapKit 位置更新而不提示位置授权。必须先调用 -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization]。
    • 你是否在 .h 文件中将 CLLocationManagerDelegate 添加到 @interface 中?
    • 另外,仅在locationManager:didUpdateLocations: 中设置mapView.showUserLocation = YES,而不是在收到授权之前。
    • 试过了。没有变化。
    【解决方案2】:

    我也遇到了同样的情况,this answer苦苦挣扎了好久才给我解决了。

    您需要将locationManager 设为控制器的属性。

    我应该注意到这发生在我使用 Swift 时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 2021-07-23
      • 2021-12-17
      • 2021-03-31
      相关资源
      最近更新 更多