hello everyone welcome to this swift and xcode tutorial my name is cal and in today's
tutorial we're going to be learning about local notifications there are two types of
notifications local notifications which are really good for things like calendar events
reminder apps that are sort of easily predictable and are just within the app and then the second
kind which we're not going to delve into today are push notifications which require a service
to sort of push the information most of the work today is centered around actually just getting
permission from the user to push notifications and if you don't have permission what to do and
how to handle it but yeah let's get started cool so we're just going to create a new xcode project
this is an iOS app i'm just going to call this notifications tutorial next and create
we're going to head into the main storyboard i'm going to drag in a couple of text fields so i'm
just going to add a new component i've searched for text field and i'm just dragging them on to
the default view controller we're also going to drag in a date picker so just setting that below
as well as a button cool next we're going to add some constraints i'm just going to give it top
space center horizontally and equal width and then i'm just going to change proportional width to 0.8
meaning that our text field will take up 80 percent of our view controller next we're
going to head in and just give our text field some placeholder text so just giving this one title our
next text field we're just going to set it below our title text field giving it exactly the same
constraints as our title text field placeholder text is going to be message and just making the
proportional width one we're going to set our date picker below our second text field we're going to
give it exactly the same constraints again finally we're just going to set our button just below our
date picker make the text size a little bit bigger and rename the text to schedule cool
next we're going to open up our assistant editor we're going to create some outlets so first thing
we're going to do is right click and drag below our view did load and create an action i'm just
calling it schedule action and then we're going to create an outlet for our date picker just calling
it date picker both of our text fields the first one i'm just calling message text field and title
text field we can close that off now and head into our view controller dot swift first thing we're
going to do is import user notifications and then we're just going to create a variable calling it
notification center is equal to un notification center and then just calling current so we're
going to use this notification center in both of our functions so that's why we're just declaring
it here and the first thing we're going to do is in our viewed load we're going to ask the user for
permission to send local notifications so we're just going to call notification center request
authorization and then options i'm going to give it alert and sound we're asking for permission to
display alerts and play some sound and next we want to fill in our completion handler it's going
to take two variables the first one is boolean which is permission granted and the second one is
any errors that happened and so what you can do in here is say if permission is not granted maybe
you need to disable some fields or whatever but i'm just going to print permission denied
as we're actually going to do most of our validation on whether or not we've
got permission in our schedule action so yeah what we're going to do is just call
notification center dot get notification settings naming our completion handler variable settings
inside here we're going to say if settings authorization status is equal to authorized
meaning the user has given us permission to send local notifications we're going to say
let content equal to un mutable notification content we're going to give it content title
is equal to we're actually going to declare this at the top we're going to say let title
equal to title text field dot text and then let message equal to message text field dot text
and we're just going to set our content title to our title and our content body to our message
and then we just need to give it an exclamation mark to force unwrap as well as calling self
finally we're going to declare one more variable we're going to call it date which is equal to our
self date picker dot date and then we're just going to say let date components equal to calendar
current date components and we need all pretty much all of the date components so i'm just
calling year month day hour and minute and from our date is our date that we created at the top
and we're going to say let trigger equal to un calendar notification trigger date matching our
date components and repeats is false i'm going to say let request equal to un notification request
for our identifier we're just going to call uuid and then just calling uuid string so that just
creates a unique string our content is going to be our content and our trigger will be our trigger
next we're just going to call self notification center add request with a completion handler of
an error we're going to say if error is not equal to nil meaning that there is an error
we're going to print out that error message as well as just returning but if we make it past
there and there's no error we're going to create an alert dialog just calling it ac is equal to a
ui alert controller we're going to give it a title of notification scheduled our message at and give
it our time and our preferred style is going to be alert let's create just a quick function
to get our date into a readable string we're just going to call it formatted date which receives a
date and returns a string we're going to say let formatter equal to date formatter and our
formatted date format is going to be days months years hours and minutes but you could really
choose whatever you like here and then return formatter string from date and giving it our date
now in our message we can just call at plus our formatted date we can remove this extra ui alert
controller hit fix to add self in front of our formatted date next we're going to add an action
to our alert controller so i'm just going to say add action giving it a ui alert action a title of
okay style default and nil for our handler so just hitting enter and then backspacing that there then
we're going to say self present ac animated is true and we can actually copy and paste this down
into our else statement because if the user hasn't given us permission to send local notifications we
need to tell them that hey if you want to use this feature you need to enable notifications
so we're just changing the messages but we also need to create an action to allow them to go to
the settings we're just going to create a variable calling it go to settings equal to ui alert action
our title we're just going to give it settings style is default this one is going to need a
handler so just going to hit enter giving it an underscore we're going to say guard let
settings url equal to url giving it the string of ui application dot open settings url string
and then just in else we're going to return so if we can't find that we're just going to return and
then we're going to say if ui application shared can open url giving it our settings urls meaning
that we can open our settings url we're going to say ui application shared open giving it our
settings url we're going to remove options and our completion handler we're just going to not do
anything and then we're just going to say ac add action and giving it our go to settings and if we
scroll back up we're getting error messages saying that views should only be modified from the main
thread to fix this we just need to say dispatch queue main async put all of our code inside this
to say we're going to put this on the main thread cool and if we build and run this now you can see
once we've opened the app our viewed load code has run and it's asking us permission to send local
notifications we're going to hit don't allow to test our settings url so we're just going to hit
settings we're going to allow notifications head back into the app set the time a little
bit in the future fill in our notification title and message and then just hit schedule
and if we close off the app and wait until it's time for our notification you can see it's popped
up there with our message and our title so yeah that's how you send local notifications in swift
and xcode i hope you enjoyed the tutorial and i'll catch you guys in the next one