Join Steven Lipton for an in-depth discussion in this video Check user permissions, part of iOS and watchOS App Development: Notifications.
- [Instructor] This pizza app is all about notifications. We should explain that to the user if they fail to give permissions. If permissions are not granted, I'll post an alert to ask for them. Otherwise, I'll schedule a notification. So let's go to the view controller, head up to the schedulePizza action which you'll find right here. Now permissions matter most when you add the notification. I'm going to check the status using the getNotificationSettings method.
It only has a closure with a parameter settings so just type in this. We get UNNotificationCenter dot and then we get current and now we're going to getNotificationSettings and it's got a completion handler so we'll do that. That gets us our closure and that gets us the notification settings we're going to call that settings.
And then we're in the code here. Now settings has a property authorization status which I'll assign to status so let's make that, let status = settings.authorizationStatus, there it is, like so. So I'll check the status for not being authorized, add this if status == .denied there's another status I may want which is that it's not determined yet and so I'm going to do status == not determined with an or on there and there's notDetermined.
And if this is the case where I've got a denied or notDetermined, I'm going to put an alert for the user. Now alerts since I'm going to using a piece of UI must run on the main thread so I have to execute it here in the correct thread and so I'm going to put a DispatchQueue.main.async and then inside that closure I'm going to put in self.accessDeniedAlert.
And if that is all true, then I'm going to put a return after that so that I'm going to pop myself out of here, okay. So that clears me of all denied and notDetermined statuses. Everything else, I'm going to go ahead and set up a notification. I actually have one already which I added to the code for you so you can start playing with them and that's the introNotification so you can type in self.introNotification to get that up. We're going to do this again in the makePizza so I can just take all this code I just wrote, copy it, and I have to do it down here in makePizza so I'm just going to paste it in there like so.
This is the basic structure I use for permission checking. Checking for permissions this way makes it easy to deal with changes by the user in permissions and respond immediately to those changes. However, since the closure is running in another thread, make sure you code your notification within the closure. If you try to code outside this closure, the permissions will not be accurate.
- Requesting user permission
- Static vs. dynamic notification content
- Sending local notifications
- Updating and removing notifications
- Creating custom notifications
- Adding categories and actions to notifications
- Pushing notifications
- Building push notification payloads: alerts and actions
- Adding service and content extensions
- Attaching rich media: images, audio, and video
- Creating watchOS notifications
Skill Level Intermediate
Apple watchOS 4 App Development Essential Trainingwith Steven Lipton3h 10m Intermediate
1. User Permissions
2. Local Notifications
3. Notification Management
4. Interacting with Notifications
5. Push Notifications
6. Push Notification Payloads
7. The Service Extensions
8. The Content Extension
9. Rich Media in Notifications
10. Apple Watch Notifications
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.