Bạn sẽ muốn sử dụng ủy quyền của CoreLocation. Ngay khi bạn nhận được một tọa độ, hãy dừng CoreLocation, thiết lập bộ hẹn giờ để khởi động lại trong 5 phút.
Với iOS 8, bạn cần đặt mục nhập plist cho NSLocationWhenInUseUsageDescription và/hoặc NSLocationAlwaysInUseDescription.
Tài liệu của Apple rất rõ ràng về cách thực hiện tất cả điều này.
-(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];
}
}
Điều này có tiếp tục hoạt động khi ứng dụng đã nhập vào nền không? – initWithQuestion
Một số chế độ có. Đọc tài liệu. – VaporwareWolf
Tôi nhận thấy rằng '' [self.locationManager stopUpdatingLocation] '' là đặc trưng hai lần, nên phương thức timerFired là '' [self.locationManager startUpdatingLocation] ''? Tôi tin rằng '' self.location = [locations lastObject] '' là thích hợp hơn vì đối tượng cuối cùng luôn luôn là gần đây nhất. Cuối cùng, bạn có thể giải thích lý do tại sao logic vô hiệu hóa bộ hẹn giờ được gọi? – initWithQuestion