From the course: iOS Development: Siri Shortcuts

Placing order using custom intent - iOS Tutorial

From the course: iOS Development: Siri Shortcuts

Start my 1-month free trial

Placing order using custom intent

- [Instructor] Currently, our CoffeeOrderDataManager is responsible for persisting the coffee orders, as well as fetching all the orders. But it doesn't really donate the order and we have to donate the order so that we can place the order using our custom intent. Let's go ahead and jump into the CoffeeOrderDataManager and add the donate order right inside the CoffeeOrderDataManager. This is the same implementation that you have done previously. The INInteraction class is not available unless and until you import intents, so let's go ahead and do that also. Great. The next step is to go into OrderTableViewController and go ahead and remove, or you can even go ahead and comment this section out. This section was only available and used for interaction purposes for development, and this is going to allow us to delete all the donations so that we don't see hundreds of donations in our Spotlight search. Let's also go ahead and remove the addOrder from the OrderTableViewController, as well as donateOrder, because all of this functionality is now part of the CoffeeOrderDataManager. Next, jump into IntentsHandler. You can see, currently, the handle function is simply returning you a success. But what we really want to do is to handle and then place the order. So let's go ahead and see how we can do that. As you can see on line number eight and line number nine, there are some errors. For line number nine, it's basically saying that it's not able to find the CoffeeOrderDataManager. Let's go ahead and select CoffeeOrderDataManager, expand the right pane, and click the checkbox indicating that it's also available in HotCoffeeintents. Do the same exact thing for the order, also. And also for the coffee class so that it can be accessed from HotCoffeeintents project. Now, let's go ahead and jump into it and build our application. Now on line number eight, you can see that it's complaining that there is no initializer for the order class that takes in the intent. Which is correct, because there isn't. Let's go ahead and add that initializer into our order class. Go to the order class and add the initializer which takes in the intent. The whole point of this particular initializer is to take in the intent and use the intent to create a new order. On line number 50, it's complaining that the coffee class does not have a function called findby. The findby function is responsible for finding a coffee using the name of the coffee and returning the instance of the coffee. We can even add that in our coffee extension. Great. Let's go back again and try to build our application. Since we are about to share data and use our defaults between two separate targets, we need to create the app groups in the HotCoffeeintents, also. Click on the HotCoffee. Go to Capabilities. You can see that, for this particular target, we have already added the app group, which is listed over here. We need to add the same exact app group for our separate target. So click on the HotCoffee intents, make sure that the App Groups is enabled, add a new app group. Paste the one and then there we go. Make sure that the name of the app group is exactly the same as you have said in the original HotCoffee target. Let's go ahead and jump into IntentsHandler and build our application now. And go ahead and give it a try by running it. Let's go ahead and start by creating our first order. We're going to order a cappuccino, large size, and order. Let's go back to the home screen and check out our donation in the Spotlight search. You can see that it's showing you the old donations. You can either use the old donations or you can wait for the new donation to show up. Sometime you will see that the donations are not going to show up, as in this particular case. But in this case, it might be our fault why the donation is not showing. Let's go back and make sure that our code reflects that whenever we are adding a new coffee, it is also donating. Let's go to the CoffeeOrderDataManager and you can see that when we are saving the order, we are not really calling donateOrder. So, let's go ahead and do that. donateOrder, pass in the order. Great. Now, let's go ahead and give it a try again. This time you can see our old order, which is cappuccino. Let's go ahead and add a new order for regular coffee, large, and order. You can see that our donation is now showing up. Click on the donation. You don't really see any interface, because we haven't really built anything. And let's go ahead and click order. Sometime you will see this kind of a bug occurring. Try to reset your device and the bug should go away. Let's go ahead and give it an order for iced coffee. And you can see that the iced coffee order went through fine. If it went through fine, then it should have created a particular order in our application. In order to do that, we have to run our application again to see the order. So let's go back to our application, stop the current instance, and run it again. And now you can see our final order, which is the iced coffee, being added. So this time we were able to place our order using a custom intent in Siri Shortcuts.

Contents