gpt4 book ai didi

iphone - 每 10 分钟发送一次数据

转载 作者:行者123 更新时间:2023-12-03 19:40:24 27 4
gpt4 key购买 nike

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




// Override point for customization after application launch.

locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//[locmanager setDistanceFilter:10];
updateTimer = [NSTimer timerWithTimeInterval:600 target:self selector:@selector(startUpdating) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:updateTimer forMode:NSDefaultRunLoopMode];

[window makeKeyAndVisible];

return YES;
}

-(void)startUpdating
{
[locmanager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (newLocation.horizontalAccuracy < 0) return;


CLLocationCoordinate2D loc = [newLocation coordinate];
currentdate=[[NSDate date]timeIntervalSince1970];
latitude = [NSString stringWithFormat: @"%f", loc.latitude];
longitude= [NSString stringWithFormat: @"%f", loc.longitude];
//Call to webservice to send data
}

我想每 10 分钟向网络服务发送一次坐标。尝试过这样做,但这不起作用。我的应用程序已注册以在后台获取位置更新。请建议我需要对此程序进行的更改。

最佳答案

我已经完成了类似的操作,使用 NSUserDefaults 记录上次发送到服务器的日期时间,并使用 NSTimeInterval 比较更新位置的日期戳和该值。我用的是 30 秒,但你可以向上调整。这有点麻烦,但它可以在后台运行等情况下使用。

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

updatedLocation = [newLocation retain];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSDate *myDate = (NSDate *)[prefs objectForKey:@"myDateKey"];

NSDate *lastDate = (NSDate *)newLocation.timestamp;
NSTimeInterval theDiff = [lastDate timeIntervalSinceDate:myDate];

if (theDiff > 30.0f || myDate == nil){
//do your webservices stuff here
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:lastDate forKey:@"myDateKey"];
[prefs synchronize];

}

关于iphone - 每 10 分钟发送一次数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4098663/

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