13

我已经检查了 iOS 10 Home 应用程序。屏幕截图仅从家庭应用程序中捕获。

在此处输入图像描述从过去 2 天开始,我一直在尝试实现 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]];

感谢您阅读我的帖子。任何想法/建议都会非常有帮助。

4

2 回答 2

4

尽管 Apple 在其 HomeKit 应用程序中提供了此功能,但公众无法使用此功能。你能做的最好的事情就是每天创建多个触发器,但这样用户就会感到困惑。

请在此处打开一个雷达错误

于 2016-09-13T16:51:40.697 回答
-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] ;
}
于 2017-08-29T10:05:24.970 回答