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

时间:2014-09-30 02:01:26

标签: ios parse-platform ios8

所以我最近更新到了与iOS 8兼容的最新Parse SDK,我添加了两个密钥NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription ...但由于某种原因[PFGeoPoint geopointForCurrentLocationInBackground]没有被调用....我不明白为什么。

enter image description here

以下是代码片段:

                     [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];                                 }
                             }];

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:6)

[PFGeoPoint geopointForCurrentLocationInBackground]只有在您第一次致电CLLocationManager时才会被呼叫,并且已要求用户允许其显示其位置。

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

我使用的是这样的东西:

// 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>>
}

您还需要实现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;
    }
}