我创建了一个 View 。在 View 中我做了函数——测量从用户到 map 上标记的距离。但是当用户打开应用程序时,仍然会警告我的应用程序使用地理定位。如何删除警告?当用户打开应用程序时,我试图停止地理定位。我找到了这个方法:applicationWillEnterForeground。但是它的例子很少。我尝试做这样的事情
if(&UIApplicationWillEnterForegroundNotification) { //needed to run on older devices, otherwise you'll get EXC_BAD_ACCESS
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(enteredForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
然而,这段代码留下了警告,只有当你点击它时它才会关闭警告。 告诉我如何在隐藏应用程序时停止定位?
PS在info.plist中我添加
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
这是警告示例
我的部分代码
-(void)setupArray{
self.myLocationManager = [[CLLocationManager alloc] init];
self.myLocationManager = [[CLLocationManager alloc] init];
[self.myLocationManager startUpdatingLocation];
self.myLocationManager.delegate = self;
[self.myLocationManager startUpdatingLocation];
CLLocation *startLocation = self.myLocationManager.location;
CLLocation *location = [[CLLocation alloc] initWithLatitude:60.050043 longitude:30.345783];
float betweenDistance=[startLocation distanceFromLocation:location];
[GMSServices provideAPIKey:@"###"];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:59.93 longitude:30.35 zoom:9];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
mapView_.myLocationEnabled = true;
mapView_.userInteractionEnabled = YES;
mapView_.frame = CGRectMake(0, 0, 200, 200);
mapView_.center = self.view.center;
[self.scrollView addSubview:mapView_];
更新 如果我写这是appdelegate
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog("app hide"); //it's work
}
但是如果我在我写属性 CCClocation 的地方写这个,它是行不通的。我不明白我需要在哪里输入 [myLocationManager stopUpdatingLocation];
最佳答案
您尝试处理错误的应用状态更改 — UIApplicationWillEnterForegroundNotification
在您的应用即将激活 时发布,即将从后台转移到前台。在您的 AppDelegate 中使用 - applicationWillResignActive:
方法或订阅 UIApplicationWillResignActiveNotification
并停止您的地理定位服务。
附言您的代码确实需要一些清理,不需要 self.myLocationManager = [[CLLocationManager alloc] init];
和 [self.myLocationManager startUpdatingLocation];
多次调用。您还应该在实例变量名称前使用下划线字符 — _mapView
而不是 mapView_
。祝你好运!
关于ios - 隐藏应用程序时停止定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30351262/