내 위치 애플리케이션에서 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 분 카운트 다운으로 타이머를 시작하고 가장 정확한 위치를 얻은 다음 다시 시작하고 반복하십시오. –
위치 관리자 개체를 초기화하고 타이머 내부에서 청취를 시작하면 응용 프로그램이 백그라운드로 이동하면 didUpdateToLocation 이벤트가 실행됩니까? – Ramprasad
백그라운드의 응용 프로그램이 활성화되어 있지 않으므로이 응용 프로그램은 실행되지 않습니다. 또는 운영 체제가 백그라운드 모드로 작동하도록 요청할 수 있지만 응용 프로그램이 VOIP 또는 탐색 응용 프로그램과 같은 특정 종류가 아닌 경우 제한된 시간 동안이 옵션이 부여됩니다. 그것들은 배경에 특권을 가지고있다. –