Creating users

When using SiriKit, it needs to have an INPerson object. An INPerson object is used by Siri to send users things—money in our case. Let's create this new file:

  1. Right-click the Misc folder and select New File.
  2. Inside of the Choose a template for your new file screen, select iOS at the top, and then Swift. Then, hit Next.
  3. Save the file as RestaurantContact.
  4. Click Create.
  5. Add the following code to this file:
import Intents 
 
struct RestaurantContact { 
    let name: String 
    let email: String 
     
    static func allContacts() -> [RestaurantContact] { 
        return [ 
            RestaurantContact(name: "Jason Clayton", email: "jason@mac.com"), 
            RestaurantContact(name: "Joshua Clayton", email: "joshua@texas.edu"), 
            RestaurantContact(name: "Teena Harris", email: "teena@gmail.com") 
        ] 
    } 
     
    func inPerson() -> INPerson { 
        let formatter = PersonNameComponentsFormatter() 
        let handle = INPersonHandle(value: email, type: .emailAddress) 
         
        if let components = formatter.personNameComponents(from: name) { 
            return INPerson(personHandle: handle, nameComponents: components, displayName: components.familyName, image: nil, contactIdentifier: nil, customIdentifier: nil) 
        } 
        else { 
            return INPerson(personHandle: handle, nameComponents: nil, displayName: nil, image: nil, contactIdentifier: nil, customIdentifier: nil) 
        } 
    } 
} 

Here, we are creating contacts that we can use to ask Siri to send money. We have set up three people to accept money at this time: Jason Clayton, Joshua Clayton, and Teena Harris. When we request with Siri, these are the names that it looks for to see if the person exists. If the name is not in this list, Siri lets you know that the name is not found. This list can have any name you wish to have, so if you want to change the names to something else, you can do that now. Just make sure that when we get to the requesting section, you change the name there as well. Our inPerson() method is just formatting into a format that SiriKit needs to be able to read the object. We now need to add code that runs when the send payment intent is invoked.