我正在实现 UILocalNotification
在晚上 8:00 提醒首次使用该应用程序的用户再次使用该应用程序。用户可以将其完全关闭或设置不同的时间。
我选择晚上 8:00 作为下类后的时间,这样它就不会那么打扰或打扰了。
我不确定如何处理时区。
如果用户在纽约。 [NSDate date]
仅返回 GMT 日期时间(自引用日期以来的某个时刻)。那么我如何确定他的晚上 8:00 呢?
我可以这样做吗?
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [calendar components:(NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitTimeZone) fromDate:[NSDate date]];
[dateComps setHour:20];
[dateComps setTimeZone:[NSTimeZone defaultTimeZone]];
NSDate *result = [calendar dateFromComponents:dateComps];
最佳答案
来自 UILocalNotification
docs :
The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock.
因此,为了确保您的通知设置在相同的“挂钟”时间(例如晚上 8 点),无论时区如何,只需执行
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = theDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
关于ios - 无论用户身在何处,如何找出用户的8 :00 PM,?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20109714/