每10分钟发送一次数据 [英] Send Data Every 10 minutes

查看:241
本文介绍了每10分钟发送一次数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 - (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分钟就连接到一次网络服务。这样做但这不起作用。我的应用程序已注册,以便在后台获取位置更新。请建议我需要对此程序进行更改。

I want to send coordinates to web service every 10 minutes.Tried doing this but this is not working.My application is registered to get location updates in background.Please suggest me changes that need to be done to this program.

推荐答案

通过使用NSUserDefaults记录上次发送到服务器的日期时间并使用NSTimeInterval比较更新后的位置的日期戳和此值,我做了类似的事情。我使用30秒,但你可以向上调整。它有点破解,但它适用于后台运行等。

I've done something similar to this by using NSUserDefaults to record the datetime it last sent to the server and using NSTimeInterval to compare between the updated location's datestamp and this value. I am using 30s but you can adjust upwards. It's a bit of hack but it works with background running etc.

- (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];

    }

这篇关于每10分钟发送一次数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆