locationManager didFailWithError 重复调用

sma*_*nja 0 iphone objective-c ios4 ios

当位置管理器无法找到当前位置时,我会显示 alertViews。我是这样做的

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

[manager stopUpdatingLocation];

switch([error code])
{
    case kCLErrorNetwork: // general, network-related error
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please check your network connection or that you are not in airplane mode" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
        break;
    case kCLErrorDenied:{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"User has denied to use current Location " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
        break;
    default:
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unknown network error" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
        break;
}

}
Run Code Online (Sandbox Code Playgroud)

我的问题是locationManager didFailWithError 方法被重复调用。所以我的警报视图反复显示。

我该如何解决这个问题?

gra*_*ver 5

苹果文档指出:

如果位置服务无法立即检索位置,则会报告kCLErrorLocationUnknown错误并继续尝试。在这种情况下,您可以简单地忽略错误并等待新事件。

如果用户拒绝您的应用程序使用位置服务,此方法将报告kCLErrorDenied错误。收到此类错误后,您应该停止定位服务。

因此,您可能会跳过某些停止更新并显示警报的情况,尤其是默认情况。