隐藏应用时停止位置

时间:2015-05-20 13:33:38

标签: ios objective-c core-location

我创建了一个视图。在视图中我做了功能 - 测量用户到地图上标记的距离。但是当用户转动应用程序时,仍然会警告我的应用程序使用地理位置。如何删除警告?我试图在用户转动应用程序时停止地理定位。我找到了这个方法:applicationWillEnterForeground.But它的例子非常少。我尝试做这样的事情

        'create a brush from a known colour
        Dim brush = New SolidBrush(Color.AliceBlue)
        'create a brush from a user defined colour
        Dim brush2 = New SolidBrush(Color.FromArgb(128, 128, 128))
        'create a pen from a known colour
        Dim pen = New Pen(Color.Red)
        'create a pen from a custom colour
        Dim pen2 = New Pen(Color.FromArgb(0, 128, 255))
        'create a pen from a brush
        Dim pen3 = New Pen(brush)

        'don't forget to dispose of your brushes/pens when finished
        brush.Dispose()
        brush2.Dispose()
        pen.Dispose()
        pen2.Dispose()
        pen3.Dispose()

但是,此代码会保留警告,只有在您单击时才会关闭警告。 告诉我如何在隐藏应用程序时停止位置?

PS在info.plist中添加

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];
}

是警告的例子

enter image description here

我的部分代码

<key>NSLocationWhenInUseUsageDescription</key>
<string></string>

更新 如果我写这是appdelegate

-(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_];

但是如果我在我的观点中写这个我写的有关CCLocation它不起作用。我不明白我需要在哪里输入- (void)applicationWillResignActive:(UIApplication *)application { NSLog("app hide"); //it's work }

1 个答案:

答案 0 :(得分:1)

您尝试处理错误的应用状态更改 - 当您的应用即将变为活动时,会发布UIApplicationWillEnterForegroundNotification,即将从后台转移到前台。使用AppDelegate中的- applicationWillResignActive:方法或订阅UIApplicationWillResignActiveNotification并停止地理定位服务。

P.S。您的代码肯定需要进行一些清理,不需要self.myLocationManager = [[CLLocationManager alloc] init];[self.myLocationManager startUpdatingLocation];多次调用。您还应该在实例变量名称之前使用下划线字符 - _mapView而不是mapView_。祝你好运!