Core Location not working in iOS 8

僤鯓⒐⒋嵵緔 提交于 2019-11-28 07:03:28

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.

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

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

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.

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

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!