在特定时间段内触发位置更新

时间:2015-10-24 13:33:55

标签: ios iphone xcode

在我的iOS应用程序中,我希望在特定时间段内触发位置更新一段时间。 例如,我希望明天下午5点到当天晚上7点开始将位置更新到服务器。

实现这一目标的最佳方法是什么?

我不是在寻找代码,而是寻找一些流程/算法来实现这一目标。

1 个答案:

答案 0 :(得分:0)

试试这个: 我希望您知道如何设置基本的CoreLocation环境,例如将locationUsageDescription添加到info.plist并启用后台模式。所以我不会解释那些,这是我的代码。您可以将此代码添加到AppDelegate或您要添加的任何ViewController。在我的情况下,我已添加到AppDelegate-

- (void)initializeLocationManager {

    /* These are dummy _startTime and _endTime, you can set up the required dateTime as per your requirement
        _startTime is set to current dateTime + 60 seconds
        _endTime is set to _start time + 60 seconds
    */
    _startTime = [NSDate date];
    // secondsToAdd - 60 seconds
    NSTimeInterval secondsToAdd = 60;
    _startTime = [_startTime dateByAddingTimeInterval:secondsToAdd];
    _endTime = [_startTime dateByAddingTimeInterval:secondsToAdd];

    NSLog(@"Start time = %@", _startTime);
    NSLog(@"endtime = %@", _endTime);

    _locationManager = [[CLLocationManager alloc] init];

    // You can use requestWhenInUseAuthorization/ requestAlwaysAuthorization depending on your requirement
    if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [_locationManager requestAlwaysAuthorization];
    }
    [_locationManager setDelegate:self];
    [_locationManager startUpdatingLocation];
    [_locationManager startMonitoringSignificantLocationChanges];
    }

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [self initializeLocationManager];
        return YES;
    }

将CLLocationManagerDelegate实现为

#pragma mark CLLocationManager Delegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"Couldn't update location, Location services are not enabled");
    }

    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways) {

        NSLog(@"Couldn't update location, Location services not authorized.");

    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    // Compare _startTime with the current dateTime
    // If _startTime is less than current dateTime, then you can start sending updates to your server

    switch ([_startTime compare:[NSDate date]]) {
        case NSOrderedAscending:

            // You can add condition to restrict the frequency of updating location
            // Like here, I have added condition to send updates every 60 seconds

            //  lastLocationUpdatedTime should be initialize before calling [self initializeLocationManager];
            //  lastLocationUpdatedTime = [NSDate date];

            // if ([[NSDate date] timeIntervalSinceDate:lastLocationUpdatedTime] > (1*60)) {
            //   lastLocationUpdatedTime = [NSDate date];
            //   NSLog(@"send location update to server");
            //   }
            NSLog(@"send location update to server");

            if ([_endTime compare:[NSDate date]] == NSOrderedAscending) {
                NSLog(@"end updates");
            //  Stop updating location
                [_locationManager stopUpdatingLocation];
            }
            break;
        case NSOrderedSame:
            // If equals you can add your logic here...
            break;
        case NSOrderedDescending:
            // If startime is greater than time [NSDate date]
            break;
        default:
            break;
    }

}