在我的位置應用中執行didUpdateToLocation
方法。此方法每秒調用一次並提供位置數據。但是我不需要每秒鐘獲取位置,我只需要每5分鐘啓動一次該方法。是否有可能做到這一點?每5分鐘取GPS位置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//initialize location lisetener on Application startup
self.myLocationManager = [[CLLocationManager alloc]init];
self.myLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.myLocationManager.delegate = self;
[self.myLocationManager startUpdatingLocation];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[INNOViewController alloc] initWithNibName:@"INNOViewController_iPhone" bundle:nil];
} else {
self.viewController = [[INNOViewController alloc] initWithNibName:@"INNOViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
/*
if(self.executingInBackground)
{
NSLog(@"Aplication running in background");
}
else
{
NSLog(@"Aplication NOT running in background");
}
*/
//NSLog(@"new location->%@ and old location -> %@",newLocation,oldLocation);
NSString *urlAsString = @"http://www.apple.com";
NSURL *url=[NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest=[NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ([data length] > 0 && error == nil) {
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Downloaded html -> %@",html);
//NSLog(@"Downloaded successfully");
}
else if([data length] == 0 && error == nil)
{
NSLog(@"Nothing downloaded");
}
else
{
NSLog(@"Error occured -> %@",error);
}
}];
}
如果您確實想每5分鐘獲取一次,那麼您可能應該使用計時器。用5分鐘的倒數計時器啓動計時器,獲得最準確的位置,然後再次啓動並重復執行此操作。 – 2013-04-24 08:37:43
如果初始化位置管理器對象並開始在定時器內部偵聽,那麼如果應用程序轉到後臺,didUpdateToLocation事件是否會觸發? – Ramprasad 2013-04-24 09:08:19
後臺中的應用程序未處於活動狀態,因此不會觸發。或者,您可以請求操作系統在後臺模式下工作,但除非您的應用程序屬於某種類型,例如VOIP或導航應用程序,否則您只能在有限的時間內授予此權限。那些有背景特權。 – 2013-04-24 09:13:47