ios - 我在 iOS 中收到错误的通知时间?

标签 ios objective-c iphone nsdate nstimezone

我在 iPhone 应用程序开发方面有点新。所以首先,如果这是一个蹩脚的问题,我会很抱歉。现在我的问题是。在我的应用程序中,用户有时钟表盘,他可以在其中选择小时和分钟。使用它小时和分钟我想根据警报系统为用户设置警报,稍后它将设置为本地通知,我的本地通知正在工作只是警报时间显示错误。到目前为止我已经这样做了

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
calendar.timeZone = [NSTimeZone defaultTimeZone];
calendar.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];

NSDateComponents *currentTimeComponents = [calendar   components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate:[NSDate date]];
currentTimeComponents.hour = [_hourlLabel.text intValue];
currentTimeComponents.minute = [_minuteLabel.text intValue];
currentTimeComponents.second = 00;
NSDate *date = [calendar dateFromComponents:currentTimeComponents];
NSLog(@"User set date: %@",date);


UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSCalendarUnitDay;
localNotification.timeZone = [NSTimeZone defaultTimeZone];

// Schedule the notification
localNotification.fireDate = date;
localNotification.alertBody = @"It has notification from xxxx";
localNotification.alertAction = @"Show me the item";
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
localNotification.soundName = @"clockalarmfrog.mp3";

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(@"User setup local notifications: %@",localNotification);

NSTimeInterval secs = [localNotification.fireDate timeIntervalSinceDate:[NSDate date]];
NSLog(@"animation time duration in NSTimeInterval: %f",secs);

现在假设用户已将下一个闹钟时间设置为下午 5:51,当前时间是下午 5:20。现在使用我的代码我得到了这个。

2015-05-29 17:20:11.626 xxxx[651:12248] 17:20:11 PM
2015-05-29 17:21:05.988 xxxx[651:12248] User set date: 2015-05-28 23:51:00 +0000
2015-05-29 17:21:06.099 xxxx[651:12248] User setup local notifications:  <UIConcreteLocalNotification: 0x7fd9a8c7de60>{fire date = Friday, May 29, 2015 at 5:51:00 AM Bangladesh Standard Time, time zone = Asia/Dhaka (GMT+6) offset  21600, repeat interval = NSCalendarUnitDay, repeat count =     UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, May 30, 2015 at 5:51:00 AM Bangladesh Standard Time, user info = (null)}
2015-05-29 17:21:06.099 xxxx[651:12248] animation time duration in NSTimeInterval: -41406.099645
2015-05-29 17:21:06.099 xxxx[651:12248] animation reverse time duration in NSTimeInterval: 44993.900355
2015-05-29 17:21:06.168 xxxx[651:12248] duration: 44993.900355 

最佳答案

改变

localNotification.timeZone = [NSTimeZone defaultTimeZone];

localNotification.timeZone = [NSTimeZone localTimeZone];

关于ios - 我在 iOS 中收到错误的通知时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30528467/

相关文章:

ios - 二元运算符 '==' 不能应用于两个 `CGAffineTransform` 操作数

ios - 带有 UINavigationController 的 UISplitViewController 作为详细信息 View : push controller via Storyboard segue

objective-c - for (NSArray *a in directory) 无法按我的预期工作

ios - 在 32 位 iPhone 上处理 64 位整数

objective-c - 填充 Objective C 中的第二个表

iphone - 尝试在 iPhone 上调试应用程序时收到消息 "A signed resource has been added, modified, or deleted"

ios - 在 Apple Developer Portal 上使用客户的 Apple 帐户

ios - xcode 7.3.1,链接器命令失败

iphone - 如何从 iPhone 应用程序中的 URL 读取 .rtf 文件

iphone - 通知问题