Core Location not working in iOS 8

2019-01-17 12:52发布

I'm trying to use significant change location monitoring in iOS 8, but the didUpdateLocations method is never called. Here is the code for setting up the location manager:

- (void)viewDidLoad
{
    [super viewDidLoad];
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    [locationManager requestWhenInUseAuthorization];
    [locationManager startMonitoringSignificantLocationChanges];
}

Despite calling requestWhenInUseAuthorization, nothing pops up to ask the user to authorize it. I have set NSLocationWhenInUseUsageDescription, and it still does not work. didChangeAuthorizationStatus and didFailWithError are also never called. EDIT: I was able to get it to ask the user to allow location services, but even if you click allow it never shows the location.

8条回答
走好不送
2楼-- · 2019-01-17 13:48

Try

  1. Background mode on in capabilities

  2. #import <CoreLocation/CoreLocation.h>

  3. <CLLocationManagerDelegate>

  4. locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:(id)self]; [locationManager requestWhenInUseAuthorization];

    Thats in the beginning of app

  5. NSLocationWhenInUseUsageDescription in plist

  6. Try to CMD + SHIFT + K

  7. Try to turn off iPhone and then turn on - he had some cash in iOS 8 - so after 5 times - app don't ask permission to Core Location - that helped me

查看更多
贪生不怕死
3楼-- · 2019-01-17 13:52

I had a similar problem migrating my app to iOS 8. As the original code as based on sample code provided by Apple https://developer.apple.com/library/ios/samplecode/GeocoderDemo/Introduction/Intro.html I checked if it had been updated and it had. The main difference is in this function the bit relevant to iOS 8, has been commented. This worked for me.

- (void)startUpdatingCurrentLocation
{
 // if location services are restricted do nothing
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || 
       [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted )
    {
       return;
    }

// if locationManager does not currently exist, create it
   if (self.locationManager == nil)
   {
        _locationManager = [[CLLocationManager alloc] init];
        [self.locationManager setDelegate:self];
        self.locationManager.distanceFilter = 10.0f; // we don't need to be any more accurate than 10m
   }

   // for iOS 8, specific user level permission is required,
   // "when-in-use" authorization grants access to the user's location
   //
  // important: be sure to include NSLocationWhenInUseUsageDescription along with its
  // explanation string in your Info.plist or startUpdatingLocation will not work.
  //
  if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
  {
      [_locationManager requestWhenInUseAuthorization];
  }

  [_locationManager startUpdatingLocation];

  [self showCurrentLocationSpinner:YES];
}
查看更多
登录 后发表回答