[iOS]捕获用户位置:澄清 [英] [iOS]capturing user location: clarification

查看:128
本文介绍了[iOS]捕获用户位置:澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以捕获用户位置并将此信息发送到我的服务器。

I've an app that capture user location and send this info to my server.

我已经启动了LocationManager服务:

I've started the LocationManager service with:

- (CLLocationManager *)locationManager {
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        isUpdatingLocation = NO;
    }
    return _locationManager; 
}

-(void) startLookingForLocation{

    [self locationManager].desiredAccuracy = kCLLocationAccuracyBest;
    [self locationManager].distanceFilter = 250;

    if([[self locationManager]respondsToSelector:@selector(setPausesLocationUpdatesAutomatically:)])
        [[self locationManager] setPausesLocationUpdatesAutomatically:YES];

    if([[self locationManager]respondsToSelector:@selector(setActivityType:) ])
        [[self locationManager] setActivityType:CLActivityTypeFitness];

    [[self locationManager] startUpdatingLocation];
    [[self locationManager] startUpdatingHeading];

    isUpdatingLocation = YES;
}

并停止:

-(void) stopLookingForLocation{

    [[self locationManager] stopUpdatingLocation];
    [[self locationManager] stopUpdatingHeading];

    isUpdatingLocation = NO;

}

因此,当我检测到用户位置的变化时:

So, when I detect a change of user location:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    [self setCurrentLocation:newLocation];

    BOOL isInBackground = NO;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        isInBackground = YES;
    }

    if (isInBackground)
    {
        UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]
                  beginBackgroundTaskWithExpirationHandler:
                  ^{
                      [[UIApplication sharedApplication] endBackgroundTask:bgTask];
                  }];

        // Send user-location to my server

        if (bgTask != UIBackgroundTaskInvalid)
        {
            [[UIApplication sharedApplication] endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }
    }
    else
    {
        // Send user-location to my server
    }

}

并且一切正常。 (我想)。

and all works correctly. (I suppose).

当app进入后台模式时,我在appDelegate中调用此方法
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[locationManager startMonitoringSignificantLocationChanges];
}

When app go into background mode, I invoke this method within appDelegate - (void)applicationDidEnterBackground:(UIApplication *)application { [locationManager startMonitoringSignificantLocationChanges]; }

当应用返回前台时:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [locationManager stopMonitoringSignificantLocationChanges];
}

但我有些疑惑。这样,如果应用程序处于关闭状态,我的应用程序会继续将位置发送到服务器吗?

But I have some doubts. In this way, if app is in closed status, my app continues to comunicate location to server?

如果我处于后台或前台状态,我怎么才能发送位置?

How I can send location only when I'm in background or foreground status?

编辑:
我也设置:

I've also set:

UIBackgroundModes

location

UIBackgroundModes location

推荐答案

没有必要使用此代码,这将在后台激活您的应用程序不得不提到项目的info.plist中所需的背景模式,值应该是App寄存器以进行位置更新。

There is no need of this code this will active your application in background for Little time you have to mention Required background modes in info.plist of the project and the value should be App registers for location updates.

仅在info.plist中设置UIBackgroundModes位置并且它可以工作在背景
这可能对你有帮助。

only Set UIBackgroundModes location in info.plist and it works in background this may help you.

//In app deligate 

#pragma mark -
#pragma mark  GPS Service

//========== start GPS SERVICE ===========
-(void)startGpsService
{

    locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=10.0;
    [locationManager startUpdatingLocation];
    [self performSelectorInBackground:@selector(sendDataToServer) withObject:nil];
}

#pragma mark -
#pragma mark CLLocation Manager Delegates

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{
      [self performSelectorInBackground:@selector(sendDataToServer) withObject:nil];

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // No need to do any thing 
   /* UIApplication *app = [UIApplication sharedApplication];
      UIBackgroundTaskIdentifier bgTask = 0;
      bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
      [app endBackgroundTask:bgTask];
       }];
*/
}

这篇关于[iOS]捕获用户位置:澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆