13

I have checked the iOS 10 Home app. The screenshot is captured from Home app only.

enter image description hereSince last 2 days I have been trying to implement the HMTimerTrigger repeat functionality. My requirement is I have to repeat the Trigger on every monday,tuesday and friday. What I found is I can add only one day(Monday or Tuesday ... but not Monday AND Tuesday) like below.

  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]];

Thank you for reading my post. Any ideas/suggestions would be very helpful.

SRI
  • 1,514
  • 21
  • 39
  • While you can not configure multiple days in one `HMTimerTrigger` instance. You can use multiple HMTimerTrigger based for triggering at different days. For this, `HMHome` has addTrigger method, which allows to add multiple triggers. In your case you should create `HMTimerTrigger` for the each day separately and add them to `HMHome` Instance. In your example you will need to add three triggers - one for Monday, one for Tuesday and one for Friday. – Ravin Aug 29 '17 at 13:54
  • Hi @Ravin you are right but the issue is we are showing al the triggers. In that case user may get confused. So that's why I am looking for the HomeKit framework method – SRI Aug 30 '17 at 05:06
  • While this is not documented in their public API, you can check with their technical support(Technical Support Incident) and confirm if there is any official way to do this. Two TSIs are available to one developer account.(https://developer.apple.com/support/technical/) --- If it is officially not supported then you could write your own code to have something like TriggerGroup and manage it with name that you pass while creating the trigger. This may help you to provide clear UI. – Ravin Aug 30 '17 at 13:39

2 Answers2

4

This functionality is not available to the public even though Apple has it in their HomeKit app. The best you could do is create multiple triggers for each day but then the user would gets confused.

Please open up a radar bug on it here

Priyantha
  • 4,839
  • 6
  • 26
  • 46
Maria
  • 4,471
  • 1
  • 25
  • 26
-1

Few years back i did same thing using notification. You cannot create multiple trigger with one instance. So you have to create it for each day. I hope this example may give you some idea.

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] ;
}
Prabakaran
  • 620
  • 1
  • 7
  • 8
  • This is related to the HomeKit but you have given answer for the notifications. Both the concepts are different – SRI Aug 29 '17 at 10:09