我在 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/