CoreLocation의 위임자를 사용하려고합니다. 좌표를 얻 자마자 CoreLocation을 중지하고 타이머를 설정하여 5 분 안에 다시 시작하십시오.
iOS 8에서는 NSLocationWhenInUseUsageDescription 및/또는 NSLocationAlwaysInUseDescription에 대한 plist 항목을 설정해야합니다.
Apple 설명서에는이 작업을 수행하는 방법이 매우 명확하게 나와 있습니다.
-(void)startUpdating{
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager startUpdatingLocation];
}
-(void)timerFired{
[self.timer invalidate];
_timer = nil;
[self.locationManager startUpdatingLocation];
}
// CLLocationDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations{
if(locations.count){
// Optional: check error for desired accuracy
self.location = locations[0];
[self.locationManager stopUpdatingLocation];
self.timer = [NSTimer scheduledTimerWithTimeInterval:60 * 5 target:self selector:@selector(timerFired) userInfo:nil repeats:NO];
}
}
앱이 백그라운드로 들어갔을 때 계속 작동합니까? – initWithQuestion
일부 모드는 yes입니다. 설명서를 읽으십시오. – VaporwareWolf
timerFired 메소드가''[self.locationManager startUpdatingLocation]''일 때''[self.locationManager stopUpdatingLocation]''이 두 번 등장한다는 것을 알았습니까? 필자는 마지막 객체가 항상 가장 최근이기 때문에''self.location = [locations lastObject]''가 더 적합하다고 생각합니다. 마지막으로 타이머 무효화 논리가 호출되는 이유를 설명 할 수 있습니까? – initWithQuestion