2 developerwab developerWab 于 2013.01.30 13:52 提问

检测用户是否拒绝使用默认位置

请问一下,检测ios的用户是否拒绝开启默认位置,能实现么?

我需要在实现检测结果,然后根据他们的选择显示不同的视图控制器。

请赐教,谢谢。

2个回答

langyufei
langyufei   2013.01.30 14:19
已采纳

假设你使用LocationManager,那么在delegate中有个方法是

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

你来检测这个error.code是什么,具体参见apple官方文档Core Location Constants Reference中CLError部分

enum {
kCLErrorLocationUnknown  = 0,         // location is currently unknown, but CL will keep trying
kCLErrorDenied,                       // CL access has been denied (eg, user declined location use)
kCLErrorNetwork,                      // general, network-related error
kCLErrorHeadingFailure,               // heading could not be determined
kCLErrorRegionMonitoringDenied,       // Location region monitoring has been denied by the user
kCLErrorRegionMonitoringFailure,      // A registered region cannot be monitored
kCLErrorRegionMonitoringSetupDelayed, // CL could not immediately initialize region monitoring
kCLErrorRegionMonitoringResponseDelayed, // While events for this fence will be delivered, delivery will not occur immediately
kCLErrorGeocodeFoundNoResult,         // A geocode request yielded no result
kCLErrorGeocodeFoundPartialResult,    // A geocode request yielded a partial result
kCLErrorGeocodeCanceled,              // A geocode request was cancelled
kCLErrorDeferredFailed,               // Deferred mode failed
kCLErrorDeferredNotUpdatingLocation,  // Deferred mode failed because location updates disabled or paused
kCLErrorDeferredAccuracyTooLow,       // Deferred mode not supported for the requested accuracy
kCLErrorDeferredDistanceFiltered,     // Deferred mode does not support distance filters
kCLErrorDeferredCanceled,             // Deferred mode request canceled a previous request

};
typedef NSInteger CLError;

developerWab
developerWab 谢谢
接近 5 年之前 回复
redCoral_
redCoral_   2013.01.30 14:51

用下面的delegates方法:

- (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error
{
    if([error code]== kCLErrorDenied)
        self.locationDenied = YES;

        switch ([error code]) {
        // "Don't Allow" on two successive app launches is the same as saying "never allow". The user
        // can reset this for all apps by going to Settings > General > Reset > Reset Location Warnings.
        case kCLErrorDenied:
            [appDelegate showAllowGPSLocationView];
        default:
            break;
    }

    self.locationDefined = NO;
}

在AppDelegate中创建showAllowGPSLocationView方法,还要访问GPS定位。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!