Xcode 6/iOS 8 Location Simulation doesn't work

2019-04-24 13:12发布

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?

5条回答
forever°为你锁心
2楼-- · 2019-04-24 13:40

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

查看更多
Viruses.
3楼-- · 2019-04-24 13:47
- (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.");
}
查看更多
老娘就宠你
4楼-- · 2019-04-24 13:51

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.

查看更多
混吃等死
5楼-- · 2019-04-24 13:54

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

查看更多
小情绪 Triste *
6楼-- · 2019-04-24 14:03

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

查看更多
登录 后发表回答