【问题标题】:iOS 8 Parse.com update and pf geopoint current locationiOS 8 Parse.com 更新和 pf geopoint 当前位置
【发布时间】:2014-09-30 02:01:25
【问题描述】:

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

这是代码的sn-p:

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

有人可以帮忙吗?

【问题讨论】:

    标签: ios parse-platform ios8


    【解决方案1】:

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

    【讨论】:

    • 我只是在一个视图控制器中执行此操作,还是在我使用位置的每个视图控制器中执行此操作?如果应用程序在一个视图控制器中询问位置并且用户说是,那么他们已经授予应用程序使用位置的权限,对吗? (前景或前景和背景)
    • 正确,一旦应用获得权限,该权限就会一直保留,直到用户在“设置”应用中更改权限。为了防止边缘情况出现问题,我在每个可能使用位置服务的 ViewController 中实现了这些方法(通过类扩展),如果你经常使用位置服务,我推荐类似的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多