我查看了iOS 10家庭应用。屏幕截图仅从Home应用程序中捕获。
自从过去两天以来,我一直在努力实现HMTimerTrigger重复功能。我的要求是我必须在每个星期一,星期二和星期五重复触发。我找到的是我只能添加一天(周一或周二......但不是周一和周二),如下所示。
unsigned flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
NSDate *fireDate = [NSDate date];
NSDateComponents *recurrenceComponents = [[NSDateComponents alloc] init];
recurrenceComponents.weekday = 2; // For monday
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:flags fromDate:fireDate];
fireDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
HMTimerTrigger *trigger = [[HMTimerTrigger alloc] initWithName:triggerName.text
fireDate:fireDate
timeZone:nil
recurrence:recurrenceComponents
recurrenceCalendar:[NSCalendar currentCalendar]];
感谢您阅读我的帖子。任何想法/建议都会非常有帮助。
答案 0 :(得分:4)
即使Apple在其HomeKit应用程序中使用此功能,公众也无法使用此功能。您可以做的最好的事情是每天创建多个触发器,但用户会感到困惑。
请在其上打开雷达错误here
答案 1 :(得分:-1)
几年前我使用通知做同样的事情。您无法使用一个实例创建多个触发器。所以你必须为每一天创建它。我希望这个例子可以给你一些想法。
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:[NSDate date]];
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate] ;
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: referenceDate];
NSArray *arrayDays = [NSArray arrayWithObjects:@"1",@"3",@"5", nil];
for (int i=0; i<[arrayDays count]; i++)
{
// set components for time 2:00 p.m.
[componentsForFireDate setWeekday:[[arrayDays objectAtIndex:i] intValue]];
[componentsForFireDate setHour: 14];
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
NSDate *firstDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification1 = [[UILocalNotification alloc] init] ;
notification1.fireDate = firstDateOfNotification;
notification1.timeZone = [NSTimeZone localTimeZone] ;
notification1.alertBody = [NSString stringWithFormat: @"Complete your daily survey"] ;
notification1.alertAction = @"go back";
notification1.repeatInterval= NSWeekCalendarUnit;
notification1.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification1] ;
}