質問

I created a small iOS app with a daily reminder / notification. The user is able to select a hour (and minutes) which he / she will be notified. Up to this point, everything works fine. If I set the interval to 1 minute for testing, the notification appears.

NSDate *scheduled = [[NSDate date] dateByAddingTimeInterval:60*1];
NSDictionary* dataDict = [NSDictionary dictionaryWithObjectsAndKeys:scheduled,@"remindMe",@"Background Notification received",@"notifyMe",nil];
[self scheduleNotificationWithItem:dataDict];

But I failed to add the hours and minutes out of my UIDatePicker. If I use reminderTime.date, the notification is set to today with given time. But I need the notification every following day at the given time until the user stop it.

Does anyone have an idea how I can accomplish this?

役に立ちましたか?

解決

You can use NSDateComponents for this task. Add one day (tomorrow) and set the hour and minute appropriately.

Example:

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
components.day += 1;
components.hour = 14; // replace this with the value from your picker
components.minute = 30; // replace this with the value from your picker

NSDate *alertDate = [calendar dateFromComponents:components];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top