简体   繁体   中英

Core Location don't run on ios simulator

My App is created by Xcode 5 for ios7, when I pressed the button "Get Location" appeared the message "MyApp would like to use your current location", now i updated Xcode and on ios8 core location don't run, missing some key to add? or some class? that in ios7 there was no need to implement?

iOS 8 CoreLocation API has changed .

So the first thing you need to do is to add one or both of the following keys to your Info.plist file:

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

Both of these keys take a string which is a description of why you need location services. You can enter a string like “Location is required to find out where you are” which, as in iOS 7, can be localized in the InfoPlist.strings file.

Next you need to request authorization for the corresponding location method, WhenInUse or Background. Use one of these calls:

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

Below code works both ios7 and ios8 .

if (_locationmanager == nil) {
    _locationmanager = [[CLLocationManager alloc] init];
}
_locationmanager.delegate = self;
_locationmanager.distanceFilter = kCLDistanceFilterNone;
if ([CLLocationManager  locationServicesEnabled]) {  // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.

if ([_locationmanager respondsToSelector:@selector(requestWhenInUseAuthorization)])           {
    [_locationmanager requestWhenInUseAuthorization];
}
[_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