Hi Alex here from rebeloper.com and in this video I am going to talk about Siri
shortcuts and how you can add your Siri shortcuts into your very own iOS app so
without further ado let's get started!
Okie-dokie! So Siri shortcuts that's a new thing in iOS 12
and today you are going to learn how to add it into your Xcode project. So we
are going to build out from scratch an app that generates a random number.
Interesting? So while we are generating a random number inside our
app we will add Siri shortcuts to our app so you can ask Siri to generate a
random number and once it does it jumps into our app and of course shows the
generated random number. So let's get started! Okay!
Let's jump into Xcode and I just minimize my test project here and create a new
project. Go to file -> new and project. Great remember you need Xcode 10 for this as
it supports iOS 12. So we are going to create a Single View Application click
on Next. And I will call this SiriShortcutsRandomNumber.
Make sure the Swift is the language of your choice. Click on Next.
I will just drop it onto my desktop here and I will create a Git Repository on my
Mac and of course you can check it out on my GitHub repo too. I will put that up
there once we are finished. Okay, let's create this.
Okay let me just move this up a little bit so you may see more clearly. Okay,
here we are. At this time we are not going to delete the Main.storyboard
because it's much easier to add these elements. Remember I always like to code
without the storyboards abut and this simple to use app it is okay. I find it
very difficult use storyboards in more complex app. So what we're going to do
here is add a label in the center and the button at the bottom of our screen.
Let's click on here and let's type in label... let me just move this a little bit
left here... and simply drag and drop it onto the bottom of the screen. Now let's
anchor this horizontally in container and vertically into container. So let's
add these two constraints and the next up select the attributes inspector and
delete the label add a zero there. And next up set the size of the label to be
100. Okay! Click on Done there you go. Next up we will add a button at the bottom of
the screen. So a button let's drag and drop that out here. Center it,
horizontally in container add one constrain and then let's add a button
constraint. Make sure that constrains to margin and height of let's see 30. Okay,
Add to do constraints that is great and let's add for title... and that's it.
Now let's connect that with out ViewController.
Okay, so what we need to do is go to our label click on control and drag it out.
Okay and that would be I mean just name this label because it's not that
important. Okay let's come back that and this one control drag the button here
and make sure it is an action. It is an action by default. So the name should be ...
Okay. let's connect! Let's go back and close this asisstent
editor and go to our ViewController and we just close this off. Now we have
our label and our tab button. Now what do we want to do when the button is taped?
Well we want to generate of course random numbers. So let's create
a new function here. I will just type it out generateRandomNumber. Great! And
let's copy this and create a new function generateRandomNumber. Let me
give you a little more space here so you may see. Okay, so in Swift 4.2 a new
generation well a new way of generating random
numbers is in effect and that is pretty cool and let me just type that out.
Let's say randomNumber = and I will create an integer. So Int and a
random and you can see here we will choose this random (in: Range) and
the range will be between 1 and 100. Great next up we need to add this random
number to the label. So label.text = and we will add here our a
randomNumber. Great! Make sure that this is a string so you
need to add that into those parentheses. Okay!
And that is it let us just select an Ottawa iPhone XR or because I have tested
it on my XR simulator let's test it in the XS. I believe will be okay.
So let's build and run and see if the random generation is ready. Okay,
let's wait for it to launch till then and we'll explain what we are going to
do next. Well we are going to add our Siri shortcut but let's test this
out. So let's generate random number. Pretty great!
We are generating random number between 1 and 100. Good stuff! Okay!
Now it's part for the fun fact of Siri shortcuts. Now first thing what you need
to do is of course enable Siri. So let's go into capabilities and scroll down and
enable Siri. So go there and click on. Make sure that the steps are checked.
Checkmarks have appeared so we are good to go. Now let's go back to our ViewController
and let's add here a new function saying... because we are going to create a new
user activity and that user activity is what the iOS will check upon and make
sure if the user activity is present a very long time let's
say we tapped on that button for a long time let's say 100 times then in the
suggestion there will be this user activity. So let's create this function
here... First of all we need to create a user
activity... And we need to add an activity type. Now this is important
because this is how the system differentiates between different user
activities. So you need to need give it a unique user identifier. It is a string so
let's create this as a really good coder. So let's create a new struct.
But first of all for the struct we are going to create a new file and let's
name this.... just copy this for future reference. Let's create this. Let me just
drag and drop it under the ViewController. So we create a new struct and
it will be named UserActivityType. and I will create here a static let and
let's call this... Now what should this a string be? Well it is pretty simple
let's just grab our bundle identifier com.Rebeloper.SiriShortcutRandomNumber
or whatever it is for you. Let's go back here and give it a (.) dot at
the end and add this GenerateRandomNumbers. So this will be the unique
identifier for that. Let's hit command + B so we get a build. And I believe we have...
Yes here it is our placeholder. So let's type in for the activity type now:
UserActivityType.GenerateRandomNumber. And this way we omit all those occasions
for typos. Okay, now let's set up our activity. Well for first let's set our
title and that will be a Generate Random Number (have those spaces in there).
Next up we need to make this activity eligible for search and protections. So
type in: activity. isEligibleForSearch = true.
and likewise activity. isEligibleForPrediction = true. Great! Now we need
to set this activity to the user as the user activity.
self.userActivity = activity and self.userActivity.becomeCurrent.
So this should be the current activity. Let's hit command+B and
building front so we see everything succeeded. Now one last step is to go
into our app delegate and just set this function... Continue user activity
restoration handler and so on. So what we do here is fetch the activity
from search from Siri and make what we want to do inside our app. And what we
are going to do is generate this random number. So let's create here a switch
statement and we have this userActivityType. Okay, and if
this is userACtivityType.GenerateRandomNumber then we generate the
random number otherwise we just print out... Now what we need to do now is grab
our ViewController and generate a new and number. Okay, so we just if let
unwrap our viewController so...
...then we have our viewController so we may easily generate our random number.
And that is it so we need to still return false here. Great! Now let's go
back to ViewController and let's add here a print statement... so we may see
something in the lock too. Okay and here we go
randomNumber. Now let's hit Command+B so we may see that everything builded
okay. So one last thing before we test this out on our simulator I don't really
know why but I just found out that you need to create a new scheme. So
let's go into manage schemes and let's edit this current scheme. And what you
want to do is a duplicate scheme. Okay, now let's go into manage schemes and select
a rather yet let's just rename this to let's say: Test of SiriShortcutsRandomNumber.
And now let's edit this and under executable so go ahead and select run
and info. Under executable set Ask on Launch. Okay! And now we can close this.
Great! Now as a good touch we are going to add an app icon so you may see that
beautiful app icon. So just go here on desktop and I already have a one.
I believe we are ready for testing. Select SiriShortcutsRandomNumber and
select your preferred simulator and and let's a build and run. Oh one thing
before we continue. Let's add our App Store icon here we need this
1024 points app icon. Okay that is done. Okay!
Now let's generate a random number and of course the more you tap on the number
the more Siri will suggest this. So let's do this a couple of times let me show
you and lock here and let me just tap, tap, tap and so on, so on, so on. Okay, now
let's go and stop this app and go into our settings. Okay, and scroll down to
Siri and you should see your shortcut here. But it isn't there yet.
Okay, that is because first of all you need to enable inside developer mode so
go back and under a developer just scroll down all the way down where till
you see Display Recent Shortcuts. Okay, let's go back to settings Siri and let
me just build and run again. Okay, tap, tap, tap. Let's test settings and there you go Generate Random Number.
Now what you can do now is add a Siri call out for this Generate Random Number.
But before I do that let me just show you how you can now already
search for this shortcut. So if I just swipe all the way to the left and I
type in here Generate you can already see Siri shortcuts
Generator Random Number. This is the title that you gave and let me just stop
this and give it a more proper and name. Okay... that will be it.
Let's build and run again and see, if the changes take effect.
Well sometimes insert changes do not really take effect immediately but if
you already had the display name set up then it would be okay. So let's stop this
and let's go to simulator and let's search. Okay!
Let'd type in Generator it didn't really change this but this should be changed
maybe if you add it or delete your app and so on. Okay, now what I wanted to show you...
let's cancel it out... is going to settings
Siri and now let's create a Siri callout.
Generate Random Number. Great! Let's click on done. And here we are here is our
phrase. Great now whenever you call Siri Generate Random Number it will go to
your app. Now let me just show you how it works and when we search for it. And that
will conclude our tutorial for today. So generate ... okay Let's click on
this and here we go. We have generated a random number. Let's go back to search again
here it is our next random number pretty a great. Now that is it for today of
course you can as many shortcuts as you wish to your app
and that is quite easy, if you know what you are doing and if you have followed
along this tutorial. Hope you enjoyed this video, if you did go ahead and give
it a thumbs up and subscribe to our Channel. Now I have a pretty serious
question for you that you will need to comment down below. What are you going to
create with Siri shortcuts? Now what is your favourite shortcut? How will you
implement this Siri shortcuts into your app? Okay see you in the comments down
below and in the next video as always!