-28

我正在实施一个需要通过 Siri 发起呼叫的 VoIP 应用程序。我能够通过 Siri 拨打电话。但问题是 - 每次启动应用程序时,虽然联系人不在应用程序的联系人列表中。

我不确定如何以及在哪里处理。我的意思是如果应用程序没有像 Skype 这样的联系人处理它,则不要启动该应用程序。Skype 回复如下:

嗯,Skype 没有找到 <用户>。

你想给谁打电话?

Bellow 是我的扩展处理程序的代码片段:

- (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);
}
4

1 回答 1

3

您可以在resolveContactsForStartAudioCall方法中处理该问题,检查您在该意图中获得的人是否包含在您的应用联系人列表中。

- (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
}

请注意,如果您想将应用程序中的联系人共享到任何扩展程序,您可能需要启用应用程序组支持。以下是一些指导方针:

  1. 苹果文档
  2. 堆栈溢出链接
于 2017-01-03T16:13:10.847 回答