问题
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 :
You add
NSLocationWhenInUseUsageDescription
&NSLocationAlwaysUsageDescription
to the plist with some string values that will be prompted to user.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.
来源:https://stackoverflow.com/questions/26777246/using-freeway-drive-city-run-in-ios-simulator-in-background-mode