How to set the repeat UILocal Notifications on the select list of Weekdays
Asked Answered
T

1

6

I have implemented UILocal Notification using the following link

http://useyourloaf.com/blog/2010/07/31/adding-local-notifications-with-ios-4.html

And i have modified it to set the repet Notifications on each day by using

//To set the repeat notification
            notif.repeatInterval = NSDayCalendarUnit;

For exampke ex Every day at 10.00 AM

But my requirement is user needsto set the notification on selected Week Days (Monday to saturDay)

why because user may have weekly holidays like (SaturDay and Sunday) / Friday - Sunday) / Some other days..

on the week offs he shouldn't fire the notifications.

So we felicitate user to set the selected Working days and the notifications will set on those days only.. once user set the notifications.

For ex:

we have list of weekday Sun, MOn, Tue, Wed, Thu, Fri, Saturday

on those user selects Monday, Tuesday,WednesDay, Thursday. and set at 10Am

Then the notification will fire on every day 10.AM of these days.

How to do it

Tactual answered 11/3, 2013 at 15:33 Comment(0)
F
8

The API for UILocalNotification is very limited in this regard - you'll have to manually schedule 4 events repeating weekly on the days that the user selects.

An example of scheduling a repeat timer for monday would look like this

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.weekday = 2; // sunday = 1 ... saturday = 7
dateComponents.hour    = 10;

UILocalNotification *notification = //...
notification.repeatInterval = NSWeekCalendarUnit;
notification.fireDate       = [calendar dateFromComponents:dateComponents];

The day numbers can be found in the NSDateComponents Class Reference

Fornof answered 11/3, 2013 at 15:52 Comment(5)
Since NSWeekCalendarUnit is now depreciated, what's the proper way to schedule weekly repeats?Thrashing
In the docs near where it shows that this is deprecated it says Use NSCalendarUnitWeekOfMonth or NSCalendarUnitWeekOfYear instead.Fornof
I've tried both of those but neither has worked for me.Thrashing
Scratch that, NSCalendarUnitWeekOfYear seems to be working properly now.Thrashing
@Luke Irvin: can you please post your working code here, Paul.s's answer looking fine, confused in getting that done in manually timer?Commix

© 2022 - 2024 — McMap. All rights reserved.