ios - 隐藏应用程序时停止定位

标签 ios objective-c core-location

我创建了一个 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>

这是警告示例

enter image description here

我的部分代码

-(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/

相关文章:

ios - iOS 9 上的 Facebook 应用邀请,最后一个 FB iOS SDK 缩小了

iphone - 球绕圈弹跳

ios - SpriteKit - 如何添加具有特定距离的对象

swift - 后台蓝牙(BLE)传输和扫描

ios - react 原生 : change background color of initial loading view

ios - 发送推送通知而不访问 UDID

ios - CLLocation 类(class)属性始终无效

iphone - UIBarButtonItem : target-action not working?

ios - Core Location 正常工作的超时时间

iphone - iPhone上的 "X would like to use your current location"背后的规则是什么?