Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UILocalNotification错误的火灾日期_Ios_Objective C_Ios7 - Fatal编程技术网

Ios UILocalNotification错误的火灾日期

Ios UILocalNotification错误的火灾日期,ios,objective-c,ios7,Ios,Objective C,Ios7,我有一个闹钟,我想运行周一、周六和周日。非常简单的东西。我使用此代码创建UILocalNotification。因为今天是Sat,所以我正在测试它,看看今天的闹钟是否响了 //User picks a time from a date picker that shows only hours and mins NSDate *selectedDate = [datePicker date]; NSCalendar *calendar = [NSCalendar currentCalendar];

我有一个闹钟,我想运行周一、周六和周日。非常简单的东西。我使用此代码创建UILocalNotification。因为今天是Sat,所以我正在测试它,看看今天的闹钟是否响了

//User picks a time from a date picker that shows only hours and mins
NSDate *selectedDate = [datePicker date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *selecDateComp = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:selectedDate];

NSInteger hour = [selecDateComp hour];
NSInteger minute = [selecDateComp minute];

//NSLog(@"hour: %d ... minute: %d ...", hour, minute);


// set components for fire time
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit  fromDate:now];


[componentsForFireDate setHour:hour];
[componentsForFireDate setMinute:minute] ;

//testing
[componentsForFireDate setWeekday:2]; //mon
[componentsForFireDate setWeekday:1]; //sun
[componentsForFireDate setWeekday:7]; //sat

NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];

NSLog(@"componentsForFireDate: %@ ... fireDateOfNotification: %@", componentsForFireDate, fireDateOfNotification);

    //I use this fireDateOfNotification date in my UILocalNotification
    UILocalNotification *notification = [[UILocalNotification alloc]  init] ;

    notification.fireDate = fireDateOfNotification ;
    notification.timeZone = [NSTimeZone localTimeZone] ;
    notification.alertBody = [NSString stringWithFormat: @"Wake Up Time!"] ;
    notification.userInfo= [NSDictionary dictionaryWithObject:titleStr forKey:titleStr];

    notification.repeatInterval = NSWeekCalendarUnit;

    NSMutableString *notifSoundStr = [NSMutableString stringWithFormat:@"%@ small.mp3", alarmSound];
    notification.soundName=notifSoundStr;

    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
问题是,如果你看看我的火柴,那是上周的事了。因此,即使今天是周六,今天也不会运行通知

componentsForFireDate: <NSDateComponents: 0xc0f9080>
Calendar Year: 2014
Month: 5
Leap month: no
Hour: 11
Minute: 16
Week (obsolete): 22
Weekday: 8 ... fireDateOfNotification: 2014-05-25 15:16:00 +0000
<代码>组件失效日期: 日历年:2014年 月份:5 闰月:没有 小时:11 分钟:16 第周(过时):22 工作日:8。。。通知日期:2014-05-25 15:16:00+0000 现在有一个疯狂的部分,如果我从通知中删除Sun,只做Sat&Mon,那么火灾时间有效,通知在今天发生。这里发生了什么事?我迷路了

...
[componentsForFireDate setWeekday:2]; //mon
[componentsForFireDate setWeekday:7]; //sat

componentsForFireDate: <NSDateComponents: 0xf8af890>
Calendar Year: 2014
Month: 5
Leap month: no
Hour: 11
Minute: 19
Week (obsolete): 22
Weekday: 7 ... fireDateOfNotification: 2014-05-31 15:19:00 +0000
。。。
[componentsForFireDate setWeekday:2]//周一
[componentsForFireDate setWeekday:7]//坐
组件失效日期:
日历年:2014年
月份:5
闰月:没有
小时:11
分钟:19
第周(过时):22
工作日:7。。。通知日期:2014-05-31 15:19:00+0000

您正在做以下几件事:

[componentsForFireDate setWeekday:2]; //mon
[componentsForFireDate setWeekday:1]; //sun
[componentsForFireDate setWeekday:7]; //sat
这不是在做你认为它在做的事情。您没有设置3个工作日,而是设置一个值,然后更改两次。最后要设置的是使用的值

要为3个不同的日子设置警报,您需要创建3个不同的通知

老实说,数据在过去这一事实与问题无关,因为您已将其设置为
notification.repeatInterval=NSWeekCalendarUnit
例如,如果我注销我得到的dateComponents

<NSDateComponents: 0x8d89c80>
    Calendar Year: 2014
    Month: 5
    Leap month: no
    Hour: 16
    Minute: 41
    Week (obsolete): 22
    Weekday: 7

日历年:2014年
月份:5
闰月:没有
时间:16
分钟:41
第周(过时):22
工作日:7
但如果我打印本地通知,它实际上会显示正确的日期-一周后

<UIConcreteLocalNotification: 0x8f49580>{fire date = Saturday, May 31, 2014 at 4:41:00 PM British Summer Time, time zone = Europe/London (GMT+1) offset 3600 (Daylight), repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, June 7, 2014 at 4:41:00 PM British Summer Time, user info = (null)}
{火灾日期=2014年5月31日星期六下午4:41:00英国夏季时间,时区=欧洲/伦敦(GMT+1)偏移3600(日光),重复间隔=NSWeekCalendarUnit,重复计数=UILocalNotificationInfiniteRepeatCount,下一次火灾日期=2014年6月7日星期六下午4:41:00英国夏季时间,用户信息=(null)}


您用于组件的格式已被弃用
NSCalendarUnit
,您应该改用
NSCalendarUnit
,这是一种更好的格式,因为它只是标识符的最后一部分。这允许您在键入时自动完成大部分工作

问题在于您在工作日误用了
。它适用于本周。执行您要求执行的操作的方法是计算从现在起需要多少天才能启动,并将其添加到当前的“每日”组件中


另一个问题是,您试图从一个本地通知中生成三个本地通知。你不能。这不是一个复杂的重复报警。这是一个带有单一日期时间的单一通知,boom。如果您想要三个通知,请发出三个通知。

如果您使用正确的缩进,阅读代码可能会更容易。仍然不是100%,但更好。我怀疑一个日期组件是否可以同时设置多个工作日。谢谢。我需要回到我的绘图板,为未来重复的日期创建3个本地通知。您建议在工作日使用NSCalendarUnit吗?