您好我正在开发一个在后台使用核心位置的应用,并通知位置通知推送用户。
我使用以下代码注册重要的位置更改。
CLLocationManager *locationManager = [[[CLLocationManager alloc] init]
autorelease];
[locationManager startMonitoringSignificantLocationChanges];
在更新到位置时运行后台任务。
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;
但只有一周的特定日期才需要此功能。是否可以将CLLocationManager的计时器设置为仅在一周中的特定日期处于活动状态?
例如,仅在星期一开启重要的位置更改,其余时间将关闭位置更改。
编辑:澄清,寻找关闭位置服务(与调用[locationManager stopMonitoringSignificantLocationChanges]相同),即。 gps符号仅在星期一激活,其他日子gps符号关闭。
答案 0 :(得分:0)
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *today = [NSDate date];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:toDay];
NSInteger weekday = [weekdayComponents weekday];
if (weekday == 2) {
// It is monday
} else {
// It is another day of the week
}