简体   繁体   中英

Core Location with Xcode 6 simulator

Core Location is not calling didUpdateLocations . I have 2 classes involved LocationManager and a View Controller. Plist is set for requestAlwaysAuthorization . Location is simulated in debug. Can anyone help me spot the error?

LocationManager.h

@interface LPLocationManager : NSObject <CLLocationManagerDelegate>

+(LPLocationManager*)sharedManager;

@property (strong, atomic) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *location;
@end

LocationManager.m

+(LPLocationManager*)sharedManager{
    static LPLocationManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[LPLocationManager alloc]init];
    });

    return sharedManager;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = 10;

    }

    return self;
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{


    self.location = [locations lastObject];

    [self setCurrentLocation:self.location];

    NSLog(@"self.location in didupdatelocation %@", self.location);

    [self.locationManager stopUpdatingLocation];

}

ViewController.m (where startUpdating is called)

- (void)refresh:(UIRefreshControl *)refreshControl {


    LPLocationManager *locationObject = [LPLocationManager sharedManager];
    NSLog(@"location object %@", locationObject);
    [locationObject.locationManager requestAlwaysAuthorization];
    NSLog(@"locationManager %@", locationObject.locationManager);
    [locationObject.locationManager startUpdatingLocation];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(getLocation:) name:@"locationNotification" object:nil];

    [refreshControl endRefreshing];

}

Figured it out.

In simulator, go to Settings -> General and scroll to Reset. Click Reset Location & Privacy.

Close simulator and rerun app. Back in Settings, go to Privacy -> Location and select Always for the app.

add these key-values in your info.plist file

<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>

after this define this macro

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

and put this code in viewWillAppear

locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    if(IS_OS_8_OR_LATER) {
        [locationManager requestAlwaysAuthorization];
    }

[locationManager startUpdatingLocation];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM