CLLocationManager startUpdatingLocation not calling locationManager:didUpdateLocations: or locationManager:didFailWithError:

前端 未结 8 1730
囚心锁ツ
囚心锁ツ 2020-12-05 06:21

I\'m trying to use the CLLocationManager framework in my iOS project to access the user\'s location but when I call

[locationManager startUpdat         


        
相关标签:
8条回答
  • 2020-12-05 07:01

    enter image description hereJust add this in info.plist

    NSLocationAlwaysUsageDescription --- I need Location

    NSLocationWhenInUseUsageDescription --- I need Location

    privacy - location usage description --- I need Location

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

    Now it will call your didUpdateToLocation definitely.

    for more details click here

    0 讨论(0)
  • 2020-12-05 07:04

    So if you are running in simulator don't forget to set location in simulator menu. It looks like if it is set to none nothing is called in delegate... I am not sure than if this can happen on real device too but probably it's simulator specific.

    0 讨论(0)
  • 2020-12-05 07:11

    You need to add below things to your project,

    1. In plist of your project add these things:

      1. Key: NSLocationAlwaysUsageDescription Type:String
      2. Key: NSLocationWhenInUseUsageDescription Type:String
    2. In the .m file with [locationManager startUpdatingLocation] add this condition :

      if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
           [locationManager requestAlwaysAuthorization];`
      

    Then only your CLLocationManager delegate methods will get called.

    0 讨论(0)
  • 2020-12-05 07:19

    Location Services work a bit differently starting in iOS 8.

    Mainly, you need to add a key NSLocationWhenInUseUsageDescription to your Info.plist file, and add a description why your app need Location, such as "Location needed to ...".

    Note that you might also have to check for iOS version. Only iOS 8 and up have the Location Manager listen to the requestWhenInUseAuthorization call.

    The link below shows more details: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

    Good luck!

    0 讨论(0)
  • 2020-12-05 07:19

    With iOS 8.0 you need to call -[CLLocationManager requestWhenInUseAuthorization ] or -[CLLocationManager requestAlwaysAuthorization] first so the user gets asked to give your app permission to use the location.

    0 讨论(0)
  • 2020-12-05 07:21

    You strongly need to check that you initialize CLLocationManager on main thread. In other case you will not get updateLocation event.

    I can't find such info in Apple docs but it works for me anyway.

    0 讨论(0)
提交回复
热议问题