简体   繁体   中英

Xcode 6/iOS 8 Location Simulation doesn't work

I've just updated to Xcode 6/iOS 8 SDK and my location service simulation in simulator started not working. It was fine before I updated (I'm currently unable to test on real device). Now, when I select a location for simulation, nothing happens. Delegate's -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations method is not called. I've restarted Xcode, cleaned the build folder, nothing changed. Why can this happen?

Since IOS 8 you need to ask for authorization before starting the CLLocationManager.

Are you calling one of these methods?

[self.locationManager requestWhenInUseAuthorization];  // For foreground access
[self.locationManager requestAlwaysAuthorization]; // For background access

If you have created the project before XCode 6, you probably also need to add the info.plist entry for the new permission.

For more details have a look at this post: Location Services not working in iOS 8

Add Below code in your method

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) 
{
    [self.locationManager requestWhenInUseAuthorization];
}

Also add Below line at your info.plist file

Key: NSLocationWhenInUseUsageDescription value:Uses current location

Using Xcode 6.3.1 I have had the location selection stop updating. The fix was to run another project, select "Simulate location > Don't Simulate Location" then build the original project again to resume normal location setting.

The other answers are correct, but I also had to reset the simulator before I could get a location, whereas it was working fine on a device. The app was initially installed on that simulator before iOS 8.

To reset your simulator :

https://stackoverflow.com/a/16195931

- (void)startLocationUpdates{
    geocoder = [[CLGeocoder alloc] init];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        [locationManager requestWhenInUseAuthorization];
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

      CLLocation *currentLocation = newLocation;
    if (currentLocation != nil) {
    }

    // Reverse Geocoding
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {

        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
                        fullAddress = [NSString stringWithFormat:@"%@,%@,%@,%@",
                           placemark.thoroughfare,
                           placemark.locality,
                           placemark.administrativeArea,
                           placemark.country];

            subtitleLocation = [NSString stringWithFormat:@"PostalCode::%@",
                                 placemark.postalCode];
        } else {
            //  NSLog(@"%@", error.debugDescription);
        }
    } ];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{

    NSLog(@"Cannot find the location.");
}

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