Core Location with Xcode 6 simulator

狂风中的少年 提交于 2019-12-13 05:15:40

问题


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

}

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/26031155/core-location-with-xcode-6-simulator

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