【问题标题】:iOS app background location access using a timer使用计时器的 iOS 应用后台位置访问
【发布时间】:2017-04-07 09:50:46
【问题描述】:

我正在寻找在应用程序处于后台时访问/停止位置服务的解决方案。我的应用程序在发送到后台时会连续定位(它可以访问连续位置)。这是应用程序功能所必需的。

所以我想知道一些事情:

  • 我的应用程序在后台运行时可以连续定位多长时间? (在操作系统杀死后台进程或类似的东西之前)

  • 如果我想添加一个计时器,说 60 分钟后应用将停止获取位置,那么正确的方法是什么?

【问题讨论】:

  • Apple 提供背景模式。

标签: ios


【解决方案1】:

后台位置更新可以使用以下代码完成:

在 Appdelegate 类中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {

        // This "afterResume" flag is just to show that he receiving location updates
        // are actually from the key "UIApplicationLaunchOptionsLocationKey"
        self.shareModel.afterResume = YES;

        [self.shareModel startMonitoringLocation];
    }

    return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {

[self.shareModel stopContinuosLocationUpdate];
    [self.shareModel restartMonitoringLocation];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {

//Remove the "afterResume" Flag after the app is active again.
    self.shareModel.afterResume = NO;

    [self.shareModel startContinuosLocationUpdate];
}

在位置更新类中,比如 LocationManager.m:

#import <CoreLocation/CoreLocation.h>

@property (nonatomic) CLLocationManager * anotherLocationManager;

- (void)startContinuosLocationUpdate
{
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

    if (status == kCLAuthorizationStatusDenied)
    {
        NSLog(@"Location services are disabled in settings.");
    }
    else
    {
        // for iOS 8
        if ([self.anotherLocationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
        {
            [self.anotherLocationManager requestAlwaysAuthorization];
        }
        // for iOS 9
        if ([self.anotherLocationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)])
        {
            [self.anotherLocationManager setAllowsBackgroundLocationUpdates:YES];
        }

        [self.anotherLocationManager startUpdatingLocation];
    }
}

- (void)stopContinuosLocationUpdate
{
    [self.anotherLocationManager stopUpdatingLocation];
}

- (void)startMonitoringLocation
{
    if (_anotherLocationManager)
        [_anotherLocationManager stopMonitoringSignificantLocationChanges];

    self.anotherLocationManager = [[CLLocationManager alloc]init];
    _anotherLocationManager.delegate = self;
    _anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    _anotherLocationManager.activityType = CLActivityTypeOtherNavigation;

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
        [_anotherLocationManager setAllowsBackgroundLocationUpdates:YES];
    }
    else if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        [_anotherLocationManager requestAlwaysAuthorization];
    }
    [_anotherLocationManager startMonitoringSignificantLocationChanges];
}

- (void)restartMonitoringLocation
{
    [_anotherLocationManager stopMonitoringSignificantLocationChanges];

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
        [_anotherLocationManager setAllowsBackgroundLocationUpdates:YES];
    }
    else if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        [_anotherLocationManager requestAlwaysAuthorization];
    }
    [_anotherLocationManager startMonitoringSignificantLocationChanges];
}


#pragma mark - CLLocationManager Delegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if(_dictLocation && [_dictLocation isKindOfClass:[NSDictionary class]])
    {
        float latitudeValue = [[RVCommon validateDataForNumber:_dictLocation[@"lat"]] floatValue];
        float longitudeValue = [[RVCommon validateDataForNumber:_dictLocation[@"lng"]] floatValue];
        CLLocation *facilityLocation = [[CLLocation alloc] initWithLatitude:latitudeValue longitude:longitudeValue];
        CLLocation *mostRecentLocation = locations.lastObject;

        CLLocationDistance distanceInMeters = [mostRecentLocation distanceFromLocation:facilityLocation];
        if (distanceInMeters <= 500.0)
        {
            //Here I am informing the server when user is within 500mts of the coordinate. 
        }
    }

    NSLog(@"locationManager didUpdateLocations: %@",locations);
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-11-02
  • 2013-03-02
  • 2012-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 2016-02-18
相关资源
最近更新 更多