iphone - locationManager didFailWithError 重复调用

标签 iphone objective-c ios ios4

当位置管理器无法找到当前位置时,我正在显示 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;
}

}

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

我该如何解决这个问题?

最佳答案

Apple 文档指出:

If the location service is unable to retrieve a location right away, it reports a kCLErrorLocationUnknown error and keeps trying. In such a situation, you can simply ignore the error and wait for a new event.

If the user denies your application’s use of the location service, this method reports a kCLErrorDenied error. Upon receiving such an error, you should stop the location service.

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

关于iphone - locationManager didFailWithError 重复调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10207138/

相关文章:

iphone - 我们如何在 iPhone 应用程序之间传输大数据

objective-c - NSMutableSet 设置减号设置不起作用

ios - 创建一个 UINavigationController 目前 2 UIViewController 在 Objective-C 中

iphone - 表格 View 中的两个文本字段都在 ScrollView 上,当键盘出现时消失 iPhone

ios - 如何将一个 IBOutlet 连接到容器 View Controller 中的 TableView Controller

objective-c - 在 Swift 中覆盖 Obj-c 类函数

ios - 选择 UICollectionView iOS 中的所有项目,甚至是不可见的单元格

android - 应用程序从一家公司转移到另一家公司和证书

iphone - Xcode 或 iOS 不会在 iPad 上使用我的 Icon-72.png

ios - Objective-C:在子类中使用继承和调用方法