ios - 无论用户身在何处,如何找出用户的8 :00 PM,?

标签 ios ios7 nsdate nscalendar nstimezone

我正在实现 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/

相关文章:

ios - 获取具有最近日期的对象

ios - 从日期为今天的数组中获取元素

ios - 在 Objective-C 中绘图

ios - uislider 值和变量,Swift 中的标签关联

ios - 如果我将 itunes Connect 开发者成员(member)资格从个人更改为公司,会发生什么情况?

ios - swift 文件未链接到 sks 文件且未更改

ios - MKAnnotationView 总是显示 infoButton 而不是 detailDisclosure btn

ios - 收到远程通知时如何让我的声音播放?

ios - 使 UITableViewHeaderFooterView 不粘

ios - 从 nsdate 获取总小时数