Siri - contact searching behaviour similar to skyp

2019-09-22 14:02发布

问题:

I am implementing a VoIP application where I need to initiate call through Siri. I was able to initiate a call through Siri. But problem is - every time application is launched though the contact is not in the app's contact list.

I am not sure how and where to handle that. I mean not to launch the application if application does not have that contact like Skype handled it. Skype replied something like:

Hmm, Skype didn't find <user>.

Who would you like to call?

Bellow is my code snippet for the Extension handler:

- (id)handlerForIntent:(INIntent *)intent {
    // This is the default implementation.  If you want different objects to handle different intents,
    // you can override this and return the handler you want for that particular intent.
    return self;
}

#pragma mark - INStartAudioCallIntentHandling

- (void)resolveContactsForStartAudioCall:(INStartAudioCallIntent *)intent
                          withCompletion:(void (^)(NSArray<INPersonResolutionResult *> *resolutionResults))completion{
    NSArray<INPerson *> *recipients = intent.contacts;
    NSMutableArray<INPersonResolutionResult *> *resolutionResults = [NSMutableArray array];
    if (recipients.count == 0) {
        completion(@[[INPersonResolutionResult needsValue]]);
        return;
    }else if(recipients.count==1){
        [resolutionResults addObject:[INPersonResolutionResult successWithResolvedPerson:recipients.firstObject]];
    }else if(recipients.count>1){
        [resolutionResults addObject:[INPersonResolutionResult disambiguationWithPeopleToDisambiguate:recipients]];
    }else{
        [resolutionResults addObject:[INPersonResolutionResult unsupported]];
    }
    completion(resolutionResults);
}

- (void)confirmStartAudioCall:(INStartAudioCallIntent *)intent
                   completion:(void (^)(INStartAudioCallIntentResponse *response))completion{
    NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartAudioCallIntent class])];
    INStartAudioCallIntentResponse *response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeReady userActivity:userActivity];
    completion(response);
}

- (void)handleStartAudioCall:(INStartAudioCallIntent *)intent
                  completion:(void (^)(INStartAudioCallIntentResponse *response))completion{
    NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartAudioCallIntent class])];
    INStartAudioCallIntentResponse *response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeContinueInApp userActivity:userActivity];
    completion(response);
}

回答1:

You can handle that in resolveContactsForStartAudioCall method, check that the person you get in that intent contains in your app contact list.

- (void)resolveContactsForStartAudioCall:(INStartAudioCallIntent *)intent
                              withCompletion:(void (^)(NSArray<INPersonResolutionResult *> *resolutionResults))completion{
        NSArray<INPerson *> *recipients = intent.contacts;
        NSMutableArray<INPersonResolutionResult *> *resolutionResults = [NSMutableArray array];
        if (recipients.count == 0) {
            completion(@[[INPersonResolutionResult needsValue]]);
            return;
        }else if(recipients.count==1){
            if([self containContact:recipients.firstObject.displayName]){
               [resolutionResults addObject:[INPersonResolutionResult successWithResolvedPerson:recipients.firstObject]];
            }else [resolutionResults addObject:[INPersonResolutionResult unsupported]];
        }else if(recipients.count>1){
            [resolutionResults addObject:[INPersonResolutionResult disambiguationWithPeopleToDisambiguate:recipients]];
        }else{
            [resolutionResults addObject:[INPersonResolutionResult unsupported]];
        }
        completion(resolutionResults);
}
- (BOOL)containContact:(NSString *)displayName {
           //fetch contacts and check, if exist retun YES else NO
}

Please note you may need to enable App Group support if you want to share contacts from your application to any extension. Here are some guidelines:

  1. Apple Document
  2. Stack Overflow link