HMTimerTrigger for selected days using NSCalendar? - ios

I am working on Homekit based application, In That Homekit supports Triggers which fires on a specific date and it can be scheduled to fire repetitively by minutes, hour, days, week and month.
I want to schedule the Trigger to fire on specific days which is selected by user like mon,tue,wed,thu only or weekends (sat, sun).
I have set the recurrence using NSDateComponents but not able to set the repeat it for specific days.
Is there any way to schedule it by recurrenceCalendar object?
HMTimerTrigger's method has this object which can be passed as parameter.
HMTimerTrigger *newTrigger = [[HMTimerTrigger alloc] initWithName:#"Morning Schedule"
fireDate:self.fireDate
timeZone:nil
recurrence:recurranceComp
recurrenceCalendar:nil];

Related

Ios repeating local notifications

Here are my problems :
Schedule local notifications between two dates every specific day of the week . example : between march 1 and march 20 every sunday
I have more than 64 local notifications to set which is the limit .
For the first problem i know there is repeatIntervals which i can set to fire notification every Sunday but how can i set them between two dates ? Do i need to calculate manually and find all the Sundays between March 1 and 20 and then set it or there is something better ?
Use the NSCalendar to get the specific dates, put in an array, and pass to the scheduleLocalNotification.
In this topic you can have a sample code of how to do it.

Knowing if the user wants the week to begin on a Sunday or Monday

There is a calendar setting in iOS that allows the user to change on what day the week-based calendar begins. I display a calendar in my app and want to be able to leverage this setting so the layout of my weekday columns matches those of the first party Calendar app.
Is there a way to do this? I've tried tapping the .firstWeekday property of NSCalendar but it always returns 1, regardless of setting.
Change your phone's region and try this:
let firstWeekday = Locale.current.calendar.firstWeekday
print(firstWeekday)
The console prints 1 when the region is en_US and 2 for en_FR (France), which correspond to Sunday and Monday respectively.

How to schedule a same local notification in swift

I just want to schedule a same local notification in Swift. For example I just want to schedule a message saying "It's time to calculate your bill for this month" and I want to send this same message again next month.
How to do this kind of local notification in Swift?
I have referenced followings but was not able to schedule a same message in next month ...
//http://thecodeninja.tumblr.com/post/89942124085/notifications-in-ios-8-part-1-using-swift-what-is
//From String to NSDate in Swift
UILocalNotification has a repeatInterval property that lets you specify how often the notification should repeat. Note that this is an NSCalendarUnit, not just an arbitrary number, so you can only make a notification repeat once per calendar unit (second, minute, hour, day, week, month, etc.). See the NSCalendarUnit documentation for more options.
For example, you can make a notification repeat every month with:
notification.repeatInterval = .CalendarUnitMonth

modeling a repeating event in a database, and using UILocalNotification to fire the event

I'm not sure how this would work, but what I'd like is something similar to Apple's alarm clock that comes with the iPhone. It basically just lets you pick a time of an alarm, name the alarm, and then you can choose how often you want it to repeat (Sunday - Saturday). Based on what you choose, the alarm fires once, or at a repeated interval.
In my Core Data model, I wasn't sure how to model that. If I were thinking in terms of just plain old objects, I would think I would have some alarm object, and one of its properties would be an array. In that array I could have the day values of Sunday-Sautrday. Then when a new alarm object is created, I would schedule a UILocalNotification for the time selected, and the days chosen. To model that in terms of database objects, I'm not sure what I'm supposed to do. I was thinking something like:
Alarm - (name/string)
Day - (Sunday - Saturday/represented by integers 0-6, 1 to many relationship from Alarm to Day)
Assuming that is ok in the database, then I'm not sure how I should go about scheduling the UILocalNotifications since I thought you could only have 64 per app. I'm thinking that I could have some mechanism to schedule the first 64 alarms possible, then when the app is opened, it would just reschedule the next upcoming 64 events. Is that how I would do that? Thanks.
Using 2 entities is overkill. I would just have the Alarm entity and have a single integer attribute on it to hold the alarm days. Outside of the entity, I would have an enumeration which defines how the alarm days number is interpreted. Something like:
typedef AlarmDays {
Monday = 0,
Tuesday = 1 << 0,
Wednesday = 1 << 1,
Thursday = 1 << 2,
Friday = 1 << 3,
Saturday = 1 << 4,
Sunday = 1 << 5
} AlarmDays;
Then you can test which days it should be on using:
if (alarm.alarmDays & Monday) {
// the alarm should fire on mondays
}
And you can use the features of UILocalNotification, such as repeatInterval so you don't need to explicitly add gazillions of notifications to the system.

EKRecurrenceRule for working days in a week

I am creating simple application that has ability to add events into iPhone Calendar. So I am playing with EKEvent's recurrenceRule. There is a class EKRecurrenceRule with very long constructor:
(id)initRecurrenceWithFrequency:(EKRecurrenceFrequency)
typeinterval:(NSInteger)interval
daysOfTheWeek:(NSArray *)days
daysOfTheMonth:(NSArray *)monthDays
monthsOfTheYear:(NSArray *)months
weeksOfTheYear:(NSArray *)weeksOfTheYear
daysOfTheYear:(NSArray*)daysOfTheYear
setPositions:(NSArray *)setPositions
end:(EKRecurrenceEnd*)end
So for example, if I am trying to create a event that will be repeated every work day in the week (except Sunday), I will use this init:
initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily
interval:1
daysOfTheWeek:[NSArray arrayWithObjects:
[EKRecurrenceDayOfWeek dayOfWeek:2],
[EKRecurrenceDayOfWeek dayOfWeek:3],
[EKRecurrenceDayOfWeek dayOfWeek:4],
[EKRecurrenceDayOfWeek dayOfWeek:5],
[EKRecurrenceDayOfWeek dayOfWeek:6],
[EKRecurrenceDayOfWeek dayOfWeek:7], nil]
daysOfTheMonth:nil
monthsOfTheYear:nil
weeksOfTheYear:nil
daysOfTheYear:nil
setPositions:nil
end:nil
but it is not working, it just repeat event every day :S
When I try use EKRecurrenceFrequencyMonthly, then it works. It repeats event every month, but not on Sunday. I reported bug to Apple, because it seems that they have a bug.
Or you have other idea?
Creating A Complex Recurrence Rule
"Days of the week. For all recurrence rules besides daily recurrence rules, you can provide an array of EKRecurrenceDayOfWeek objects that indicate the days of the week on which the event occurs.
For example, you can provide an array containing EKRecurrenceDayOfWeek objects with day of week values of EKTuesday and EKFriday to create a recurrence that occurs every Tuesday and Friday."
In other words, what you want to do is use Monday to Friday and then repeat that WEEKLY. Repeating Monday to Friday every day makes no sense.
Apple documentation says:
#method initRecurrenceWithFrequency:interval:daysOfTheWeek:daysOfTheMonth:monthsOfTheYear:weeksOfTheYear:daysOfTheYear:setPositions:end:
#abstract The designated initializer.
#discussion This can be used to build any kind of recurrence rule. But be aware that certain combinations make no sense and will be ignored. For example, if you pass daysOfTheWeek for a daily recurrence, they will be ignored.
I think, we can't say it daily and say not on Sunday. Please let me know if I am mistaken.
Thanks.
initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily
interval:1
daysOfTheWeek:[NSArray arrayWithObjects:
[EKRecurrenceDayOfWeek dayOfWeek:2],
[EKRecurrenceDayOfWeek dayOfWeek:3],
[EKRecurrenceDayOfWeek dayOfWeek:4],
[EKRecurrenceDayOfWeek dayOfWeek:5],
[EKRecurrenceDayOfWeek dayOfWeek:6],
[EKRecurrenceDayOfWeek dayOfWeek:7], nil]
daysOfTheMonth:nil
monthsOfTheYear:nil
weeksOfTheYear:nil
daysOfTheYear:nil
setPositions:nil
end:nil
In this code you are using EKRecurrenceFrequencyDaily and again specifying the days of a week. Instead of this try to execute with EKRecurrenceFrequencyWeekly
EKRecurrenceDayOfWeek *weekReferecne=[[EKRecurrenceDayOfWeek alloc]initWithDayOfTheWeek:2 weekNumber:0];
and create EKRecurrenceDayOfWeek objects with week number 0(zero) so that the EKAlarm repeats every day until you specify the enddate.. This code works. Have a Happy coding

Resources