简体   繁体   English

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

[英]iOS 8 Parse.com update and pf geopoint current location

So I recently updated to the latest Parse SDK that's compatible with iOS 8 and I add the two keys NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription...but for some reason [PFGeoPoint geopointForCurrentLocationInBackground] is not being called....and I don't understand why. 所以我最近更新到与iOS 8兼容的最新Parse SDK,我添加了两个密钥NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription ...但由于某种原因[PFGeoPoint geopointForCurrentLocationInBackground]没有被调用....我不明白为什么。

在此输入图像描述

and here is a snippet of the code: 这是代码的片段:

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

Can someone help please? 有人可以帮忙吗?

[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. 只有在您第一次调用CLLocationManager并且已经要求用户显示其位置的权限时,才会调用[PFGeoPoint geopointForCurrentLocationInBackground]

You also need to update the (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status method for iOS 8. 您还需要为iOS 8更新(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status方法。

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

    //<<PUT YOUR CODE HERE AFTER LOCATION IS UPDATING>>
}

You also need to implement the locationmanager delegate method to handle changes is location authorization status, like so: 您还需要实现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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM