-->

Using freeway drive/ city run in iOS simulator in

2019-08-05 14:19发布

问题:

I am trying to make use of the options within iOS simulator : debug->freeway drive/ city run in order to simulate the location updates.

In my code I am using CLLocationManager for getting location updates with following code:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager setDistanceFilter:20];
}

-(void)viewWillAppear:(BOOL)animated {
[locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)lm didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
NSLog(@"Location returned: %f, %f Accuracy: %f", location.coordinate.latitude,  location.coordinate.longitude, location.horizontalAccuracy);
}

I am never getting a callback on the delegate for location updates, while my app is in background and i am selecting the option in simulator.

I have provided my app the background mode for location updates. Please let me know how exactly to use these features or if i am missing anything here.

回答1:

I finally sorted out the problem. The simulator's options are working perfectly fine but it was the implementation of CLLocation which was the problem.

On iOS 8 the location update code will not work unless :

  1. You add NSLocationWhenInUseUsageDescription & NSLocationAlwaysUsageDescription to the plist with some string values that will be prompted to user.

  2. You need to add ask user's permission for getting the location codes to work:

     [self.locationManager requestWhenInUseAuthorization]
     [self.locationManager requestAlwaysAuthorization]
    

Taken from this post.