iOS 8 Parse.com update and pf geopoint current location

后端 未结 1 1442
甜味超标
甜味超标 2021-01-20 06:53

So I recently updated to the latest Parse SDK that\'s compatible with iOS 8 and I add the two keys NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription..

1条回答
  •  孤城傲影
    2021-01-20 07:01

    [PFGeoPoint geopointForCurrentLocationInBackground] will only be called if you have first called your CLLocationManager engaged and have asked the user for permission to show their location.

    You also need to update the (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status method for iOS 8.

    Something like this is what I use:

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

    You also need to implement the locationmanager delegate method to handle changes is location authorization status, like so:

    - (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;
        }
    }
    

    0 讨论(0)
提交回复
热议问题