iOS 8 Parse.com更新和pf geopoint当前位置

shr*_*day 3 ios parse-platform ios8

所以我最近更新到最新的SDK解析这是与iOS 8兼容,我添加了两个键NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription ...但由于某些原因[PFGeoPoint geopointForCurrentLocationInBackground]不被称为....我不明白为什么.

在此输入图像描述

这是代码的片段:

                     [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
                         if(!error){
                             NSLog(@"Got current geopoint!");

....


else{
                                     UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Oops!" message:[error description] delegate:self cancelButtonTitle:nil otherButtonTitles:@"Okay", nil];
                                     [errorAlert show];                                 }
                             }];
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

Rya*_*ger 6

[PFGeoPoint geopointForCurrentLocationInBackground]只有在您第一次打电话给您CLLocationManager并且已经要求用户许可显示其位置时才会被呼叫.

您还需要更新(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)statusiOS 8 的方法.

这样的东西就是我用的东西:

// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}
else {
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];

    //<<PUT YOUR CODE HERE AFTER LOCATION IS UPDATING>>
}
Run Code Online (Sandbox Code Playgroud)

您还需要实现locationmanager委托方法来处理更改是位置授权状态,如下所示:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    switch (status) {
        case kCLAuthorizationStatusDenied:
            NSLog(@"kCLAuthorizationStatusDenied");
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Location Services Not Enabled" message:@"The app can’t access your current location.\n\nTo enable, please turn on location access in the Settings app under Location Services." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alertView show];
        }
        break;
    case kCLAuthorizationStatusAuthorizedWhenInUse:
    {
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters;
        [locationManager startUpdatingLocation];

        CLLocation *currentLocation = locationManager.location;
        if (currentLocation) {
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [appDelegate setCurrentLocation:currentLocation];
        }
    }
        break;
    case kCLAuthorizationStatusAuthorizedAlways:
    {
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters;
        [locationManager startUpdatingLocation];

        CLLocation *currentLocation = locationManager.location;
        if (currentLocation) {
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [appDelegate setCurrentLocation:currentLocation];
        }
    }
        break;
    case kCLAuthorizationStatusNotDetermined:
        NSLog(@"kCLAuthorizationStatusNotDetermined");
        break;
    case kCLAuthorizationStatusRestricted:
        NSLog(@"kCLAuthorizationStatusRestricted");
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)