2017-04-07 23 views
1

我正在尋找解決方案來訪問/停止位置服務,同時應用程序在後臺。當我的應用發送到後臺時,我的應用需要連續的位置(它可以訪問連續的位置)。這是應用程序功能所必需的。使用計時器的iOS應用程序後臺位置訪問

所以我想知道幾件事情:

  • 多久我的應用程序可以拍攝連續的位置,而它仍然在後臺? (之前的操作系統殺死後臺進程或類似的東西)

  • 如果我想添加一個計時器說60分鐘後應用程序將停止採取的位置,什麼是正確的方法?

    在AppDelegate類:

+0

蘋果提供背景模式。 – Lumialxk

回答

1

背景位置更新用可以使用下面的代碼來完成

- (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); 
} 
+0

也要經過這些問題:http://stackoverflow.com/q/27742677/5997339,http://stackoverflow.com/q/19042894/5997339 – ron27

+0

http://stackoverflow.com/a/43274101/5997339 – ron27

相關問題