2.4 SiriKit: Having a Conversation
In this lesson, I'll show you how to complete the workflow of a SiriKit interaction. You can see me have a real conversation with my digital assistant!
1.Introduction2 lessons, 04:21
2.What's New in iOS 10?5 lessons, 32:38
3.Conclusion1 lesson, 00:51
2.4 SiriKit: Having a Conversation
Hi, and welcome back to what's new in iOS 10. In this lesson, we are finishing our circuit extension to book some rides. Actually, before we get to the confirmation phase, I want to change the resolution phase a little bit. In the final app, the drop off location will be mandatory. I'm also adding another resolution method with a party size. Our fake service only offers rides up to four people. As always, if there is no value present, we are requesting it. In the case that there is a value, I'm checking if the number of people is less or equal to four. If so, we can call success and return the number of people. Otherwise, I'm calling the completion block with unsupported, indicating that we don't support this number of people. Next comes the confirmation phase. First we need to generate a response code. In my fake rides class, I have a method that determines if a pickup is within range. I'm going to use that in a switch statement. It depends on the pickup location. If there is a ride in range, I set it to ready. Otherwise there is no service in area. Now that we have a response code, we can generate a response object with this code and again with no user activity. We need to pass this to the completion block. Let's try it and talk to Siri. Book me a ride with TutsplusCourses. [SOUND] >> Great. Confirming you want to use TutsplusCourses? [SOUND] >> Yes. >> [SOUND] Where should they pick you up? >> [SOUND] Vienna International Center. >> Where do you want to be dropped off? [SOUND] >> Vienna Airport. >> [SOUND] How many passengers are there? >> Six. >> [SOUND] Sorry, TutsplusCourses doesn't support six passengers. How many passengers are there? [SOUND] >> Four. [SOUND] >> Sorry, TutsplusCourses doesn't offer service in your area. >> As you have heard, Siri understood that we don't support more than four passengers. It corrected us and we were able to continue with a valid number. Now it's time to handle the final phase, execution. First we need a pick up and a drop off location. We can extract the data from the intent. If we don't have one, I'm responding with failure and no use activity and return the method. Otherwise we can request the ride. Let's first create a response variable. Then I'm requesting rides from the fake ride service. If we have a ride, we have consent. We need to compose a status. This is part of the API of type high end ride status. The first property is the ride identifier. In our case I simply set it to the driver's name. Then the phase which can be a whole or idea of different states that go beyond the scope of our example. I'm using Confirm. Next is the vehicle. It is an object provided by the API. We are going to extend the ride class and the minutes to provide it. The status also wants a driver which is also a special API object. Then there is the estimated pick up date. Normally, this is provided by our service. I'm just going to return a date that is seven minutes in the future. Finally I can also attach a pick up and drop off locations to the status. Now I need to create a response. The code for it is success. And as always, no user activity. Since I have a status, I can set the ride status property to it on the response. If it didn't get a ride I'm setting the response to NoService. Then we can call the completion block. Now we need the extensions for the ride and driver. For the first one I'm returning the INRideVehicle object. Since Apple is very traditional in its understanding on how a ride can look like, all those fields are engineered to describe a car. Besides the location, it has a manufacturer property and the RegistrationPlate. Finally, it also offers the possibility to add a mapAnnotationImage. I'm going to set this to the ride image. But we need an inImage so I'm going to create an extension for that on your inImage shortly. Before I do so, I want to finish the intent driver property. That will work the same way as the vehicle does. But can be initialized with a single method call. A driver has a person handle which we are going to set to the name. I'm ignoring the name components. Let's set the display name to the driver's name as well. The driver's image will be INImage again and a driver can also have a rating which needs to be a string. The initializer also wants a phone number which I will set to none again. Now I need the UI image extension to create the inImage. It is very easy. I'm going to wrap the PMG representation of the image in the inImage object. To make those extensions available in our intent, we need to add them to the target. And now, I'm going to have my last conversation with Siri about booking a ride. Book me a ride with TutsplusCourses. >> [SOUND] Great. Confirming you want to use TutsplusCourses? [SOUND] >> Yes. >> [SOUND] Where should they pick you up? [SOUND] >> Vienna State Opera. [SOUND] >> Where do you want to be dropped off? [SOUND] >> Vienna airport. [SOUND] >> How many passengers are there? [SOUND] >> Just one. [SOUND] TutsplusCourses can be there. Do you want to request it? [SOUND] >> Yes. [SOUND] >> Your TutsplusCourses driver Sarah will be there in 7 minutes driving an Awesome Car. >> As you have seen, it isn't that difficult to get a conversation with Siri started. It is however quite difficult to make Siri understand what you want and there may be some errors. The Siri engine is also very different from device to device. Siri understood that I want to book a ride but it never understood that I want to do it with TutsplusCourses. Since it was the only app that offered rides, it suggested it to me. On the iPhone 7 which I was unable to record from, Siri correctly understood the app name every time. This concludes our adventure of SiriKit. In the next lesson, we are going to look at the final topic of custom and interactive notifications. See you there.