Good articles to my personal technology blog: https://cainluo.github.io/14977885999456.html


Siri

Siri, known as apple’s father’s voice assistant built into iOS, has been in the spotlight since the iPhone 4S debuted in 2011.

But not for long, as Siri’s functions and permissions are too weak, slowly forgotten, at least MY iPhone friends will turn Siri off, according to their view is to save electricity… I’m drunk, too.

Several years later, Siri finally welcomed the spring. With the launch of iOS 10, Siri Kit was finally opened to developers, as well as Siri Intents Extension. The fastest App used in China is still the products of BAT giant, such as QQ.

The project here is the Objective-C version, if you want to see the Swift version, you can check it out here.


Types of Siri services

Here’s a quick list of the types Siri currently supports. Please refer to the official documentation for more details.

Siri Service Types Types of Intent
VoIP Voice – VoIP Calling INStartVideoCallIntent, INStartAudioCallIntent
Message – Messaging INSendMessageIntent
Payment – Payments INSendPaymentIntent, INRequestPaymentIntent
Image – Photos INSearchForPhotosIntent
Fitness Workouts – INEndWorkoutIntent、INPauseWorkoutIntent 、INStartWorkoutIntent 、 INResumeWorkoutIntent 、INCancelWorkoutIntent
— Ride booking INRequestRideIntent, INGetRideStatusIntent, INListRideOptionsIntent, INGetRideStatusIntent
Car state – Car Commands INSetCarLockStatusIntent, INGetCarPowerLevelStatusIntent, INActivateCarSignalIntent
Car system – CarPlay INSetAudioSourceInCarIntent, INSetClimateSettingsInCarIntent
Reservations at Restaurant reservations INBookRestaurantReservationIntent

New Project

I’ll skip the new project here and go straight to adding Siri Intents Extension:

Note here that we will Include Include UI Extension as well, but there will be a whole article devoted to it.

Once created, we can see that the Siri Intents Extension is different from the previous extensions we encountered:

There is, we open the Siri Intents the Extension of the Info. See IntentsSupported and IntentsRestrictedWhileLocked file, what’s the use?

  • IntentsSupported: Our project need to support Siri Intents in here, we see INSendMessageIntent in engineering, for example, INSearchForMessagesIntent, INSetMessageAttributeIntent.

  • IntentsRestrictedWhileLocked: need to lock in the lock screen condition of Siri Intents in here, for example, we see INSendMessageIntent in engineering.


SiriKit process

Here’s what the official line on SiriKit’s process says:

Siri and Maps interact with our application through Intents Extension, where objects of type INExtension play a key role in the Intents Extension to respond to user requests directly with Siri objects. When we implement Intents Extension and generate a Siri request, a typical Intent event is handled in three steps: Resolve, Confirm, and Handle:

  • ResolvePhase. inSiriAfter receiving the user’s voice input, generate oneINIntentObject to extract key information from speech and fill the corresponding attributes. This object will be passed to us laterINExtensionThe subclass object is processed according to the different services that the subclass complies withprotocolTo choose a different solution.
  • ConfirmPhase. Pass in the previous stagehandler(for intent:)Return processingintentObject, which this stage calls in turnconfirmThe first instance method to determineSiriWhether the filling information is complete. The matching judgment results includeExactly one match,Two or more matchesAs well asNo matchThree cases. This process can letSiriAsk the user for more specific parameter information.
  • inconfirmAfter the method is executed,SiriProceed to the final processing stage, generate the reply object, and approach thisintentThe object validates the processing result and displays it to the user.


List of interaction

Here we first create a list that simulates sending messages:

Here’s another NSString category for converting pinyin:


Exact matching and fuzzy matching

Going back to our intenthandler. m file, here we’re going to start writing code that requires both an exact user name match and an obscure user name match:

    // Iterate over the options to be matched
    for (INPerson *recipient in recipients) {
        
        // Implement your contact matching logic here to create an array of matching contacts
        NSMutableArray<INPerson *> *matchingContacts = [NSMutableArray array];
        
        // The name to be matched
        NSString *recipientName = recipient.displayName;
        NSString *recipientNamePinYin = [recipientName cl_PinYin];
        
        // Exactly match the user in the list
        UserList *user = [UserList checkUserWithName:recipientName];
        
        if (user) {
            
            NSLog(@" Match to exact user :%@", user);
    
            // Create a matched user
            INPersonHandle *personHandle = [[INPersonHandle alloc] initWithValue:user.userAddress
                                                                            type:INPersonHandleTypeEmailAddress];
            INImage *iconImage = [INImage imageNamed:user.userIcon];
            
            INPerson *person = [[INPerson alloc] initWithPersonHandle:personHandle
                                                       nameComponents:nil
                                                          displayName:recipientName
                                                                image:iconImage
                                                    contactIdentifier:nil
                                                     customIdentifier:nil
                                                              aliases:nil
                                                       suggestionType:INPersonSuggestionTypeSocialProfile];
            
            // Record the matched users
            [matchingContacts addObject:person];
        }
        
        // If the user is not exactly matched, fuzzy matching is used
        if (matchingContacts.count == 0) {
            
            NSLog(@" No match for exact user :%@", user);

            for (UserList *user in [UserList userList]) {
                
                // Matches the user's name
                NSString *userName = user.userName;
                NSString *userNamePinYin = [userName cl_PinYin];
                
                if ([recipientName containsString:userName] || [recipientNamePinYin containsString:userNamePinYin]) {
                    
                    // Create a matched user
                    INPersonHandle *personHandle = [[INPersonHandle alloc] initWithValue:user.userAddress
                                                                                    type:INPersonHandleTypeEmailAddress];
                    
                    INImage *iconImage = [INImage imageNamed:user.userIcon];
                    
                    INPerson *person = [[INPerson alloc] initWithPersonHandle:personHandle
                                                               nameComponents:nil
                                                                  displayName:userName
                                                                        image:iconImage
                                                            contactIdentifier:nil
                                                             customIdentifier:nil
                                                                      aliases:nil
                                                               suggestionType:INPersonSuggestionTypeSocialProfile];
                    
                    // Record the matched user[matchingContacts addObject:person]; }}}Copy the code


Pay attention to

Note that even though we’ve written the code, we still need to see if -objc has been added to the project

NSString+PinYin. M and userlist. m:

Otherwise “_OBJC_CLASS_$_UserList”, referenced from: error.

Before running it, you must go to Settings -> Siri -> Enable Siri to use it. I’m using an emulator here, so I can’t try sending an email:

But it’s ok to send a message.


conclusion

Finally, let’s take a look at an additional article about how Penguin adapted QQ for SiriKit.


The project address

The address of the project: https://github.com/CainRun/iOS-10-Characteristic/tree/master/9.Siri%20Intents


The last