问题描述
- 检测用户是否拒绝使用默认位置
-
请问一下,检测ios的用户是否拒绝开启默认位置,能实现么?我需要在实现检测结果,然后根据他们的选择显示不同的视图控制器。
请赐教,谢谢。
解决方案
假设你使用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;
解决方案二:
用下面的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定位。
时间: 2024-12-31 16:26:48