2016-08-12 7 views
13

私はiOS 10 Homeアプリをチェックしました。スクリーンショットはホームアプリからのみキャプチャされます。複数の日にHMTimerTriggerを繰り返します(例:毎週月曜日、水曜日、iOS 10 Homeアプリのように)

enter image description here私は過去2日間、HMTimerTriggerの繰り返し機能を実装しようとしています。私の必要条件は、毎週、火曜日、金曜日にトリガーを繰り返さなければならないということです。私が見つけたのは、以下のように1日だけ(月曜日または火曜日...しかし月曜日と火曜日は追加できない)です。

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

1つの 'HMTimerTrigger'インスタンスで複数の日を設定することはできませんが、別の日にトリガーするために、複数のHMTimerTriggerを使用することができます。 このため、 'HMHome'にはaddTriggerメソッドがあり、複数のトリガーを追加できます。あなたの場合は、毎日別々に 'HMTimerTrigger'を作成し、' HMHome'インスタンスに追加してください。あなたの例では、月曜日、火曜日、金曜日の3つのトリガーを追加する必要があります。 – Ravin

+0

こんにちは@Ravinあなたは正しいですが、問題は私たちはすべてのトリガーを表示しています。その場合、ユーザーは混乱するかもしれません。だからこそ私はHomeKitフレームワークメソッドを探しているのです – SRI

+0

これは公開APIには書かれていませんが、テクニカルサポート(テクニカルサポートインシデント)で確認し、これを行う公式な方法があるかどうか確認できます。 1つの開発者アカウントで2つのTSIが利用可能です(https://developer.apple.com/support/technical/)。正式にサポートされていない場合は、独自のコードを記述してTriggerGroupのようなものを作成し、トリガーを作成するときに渡す名前。これにより、明確なUIを提供するのに役立ちます。 – Ravin

答えて

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] ; 
} 
+0

これはHomeKitに関連していますが、あなたは通知の回答を与えています。両方のコンセプトが異なる – SRI

関連する問題