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:30

into .plist file add enter image description here

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];
    }
查看更多
三岁会撩人
3楼-- · 2019-01-17 13:38

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.

查看更多
4楼-- · 2019-01-17 13:39

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.

查看更多
\"骚年 ilove
5楼-- · 2019-01-17 13:40

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.

查看更多
Emotional °昔
6楼-- · 2019-01-17 13:47

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.

查看更多
混吃等死
7楼-- · 2019-01-17 13:48

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

enter image description here

查看更多
登录 后发表回答