gpt4 book ai didi

ios - 在特定时间段的特定时间触发位置更新

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:40 28 4
gpt4 key购买 nike

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

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

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

最佳答案

试试这个:我希望您知道如何设置基本的 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;
}

}

关于ios - 在特定时间段的特定时间触发位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33318796/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com