-->

Core Location not working in iOS 8

2019-01-17 13:01发布

问题:

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.

回答1:

There is currently a bug in iOS8 beta5 which always deactivate geolocation service for your app (at least for mine).

Go in settings > Privacy > Location services > Your app > Always.

But I don't know why, but even if you set it to Always this setting will auto-deactivate, so please be patient and often return in the settings to configure again your app location.



回答2:

Try declare CLLocationManager *locationManager in your header file then it works. Unless I have declare it as a global variable it does not ask for permission and update location.

.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}

.m

- (void)viewDidLoad {
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:(id)self];
    [locationManager requestWhenInUseAuthorization];
    [locationManager startMonitoringSignificantLocationChanges];
    [locationManager startUpdatingLocation];
}

Delegate methods

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    NSLog(@"Getting Location");
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"%@", error.localizedDescription);
}

info.plis



回答3:

into .plist file add

locationManager = [[CLLocationManager alloc] init];
            [locationManager setDelegate:self];
            [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

if(IS_OS_8_OR_LATER)
    {
 if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
            {
                [locationManager requestWhenInUseAuthorization];
            }
}

[locationManager startUpdatingLocation];


 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
     currentLocation = [locations lastObject];
    }


回答4:

All the steps look complete but run :startUpdatingLocation only after you check for :didChangeAuthorizationStatus.

At this point, please make sure that the authorizationStatus is not kCLAuthorizationStatusNotDetermined or kCLAuthorizationStatusDenied.

If it is either, check for the string entries in info.plist.

If you feel all of it is correct, Make sure that you uninstall the app from the device. Perform a clean build and then run the app on the device.



回答5:

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];
}


回答6:

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



回答7:

The probable reason, why the authorization dialog is not shown with the OPs code, is that the location manager has to hang around for it to work.

Use a property or a static variable to keep locationManager in memory.



回答8:

Maybe its because of the WhenInUseAuthorization is not enough for the "Significant Location Change" updates.

When i use AlwaysAuthorization, it just works.

i cannot find it in the docs. But you can check http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/. Under "Authorization Types" heading, it states that significant location updates need AlwaysAuthorization.