From the course: iOS and watchOS App Development: Notifications

Calendar notification triggers

From the course: iOS and watchOS App Development: Notifications

Start my 1-month free trial

Calendar notification triggers

- [Instructor] The second type of notification is a calendar notification. If you have a date and time you want to launch the notification, this is the one to use. Scroll to schedule pizza method up above, and we're going to add some stuff here. We're going to use a UN calendar notification trigger, and I'm going to put it right after where it says time to make a pizza, and I'm going to do a let trigger equal UN calendar notification trigger, and I'm going to use the date matching repeats version of this, and for the date components, I'm going to change this to date components. Then I'm going to move over here to repeats, and we're going to do that false again, like so. Now this should give me an error, because date components wasn't actually defined yet. This date matching parameter, is a date as a date components object. For now, we've set it to date components. So this date components, is a date components object. Date components breaks a date into a series of components you can use to set a date. For a quick review of date components, you can check out an iOS Development Tips Weekly in the library on the subject. You'll most likely convert a date to a date component like this, which I'm going to stick above the trigger, and what you do, is I'm going to do var date components equals, and this is an object calendar, go to the current calendar, and you get the current calendars date components. So you're getting, in all intents and purposes, I'm going to get the hour, minute, and seconds from the current date. So this is the current calendar, I'm going to check its date components, it's going to ask me for a set of date components I'm interested in. I'll put a brace so I can put the set in here, and I'm going to go for hour, minute, and second, so I have a time, and I'm going to base that on the current date and time, which I can get from just instantiating a date object right into here. So what that'll do, is this'll make this be this value here. Now, I'm running into a little bit of a problem here. I would usually put some date in here, so you know, you say July fourth or something like that, or, I don't know, September 31st or something like that for a date that you'd put in here, so you could schedule something. And it's really meant for that kind of calendar scheduling, however, with a course like this, it's hard to set up a date that would work for everybody so that you could see it work, and, for that reason, I'm going to do something that will allow this date components just start a time 15 seconds into the future. Very similar to what the time intervals really doing, and that is date components dot second equals date components dot second, and you'll notice that is an optional, so I'm going to have to unwrap it, and then its integer, so I'm going to use 15 seconds here, like that. And that takes care of that, now we just get the warning that I haven't used trigger yet, and that sets up the second of the time based triggers. They're not that difficult to use, once you set them, they go off on their own. Calendar components are your way of setting times in the future, that don't make sense setting them with time intervals. The last trigger is a location trigger, and that uses a coordinate and range around a coordinate. Since that one requires a good understanding of core location, I'm going to skip it in this course.

Contents