【问题标题】:How to filter contacts using a set of phone numbers with CNContactPickerViewController?如何使用 CNContactPickerViewController 使用一组电话号码过滤联系人?
【发布时间】:2016-05-27 10:47:37
【问题描述】:

我尝试使用一组现有电话号码从通讯录中过滤一些联系人。 CNContactPickerViewController 应该在这些联系人的现有电话号码中有匹配的电话号码时禁用选择这些联系人。

我需要使用电话号码作为过滤器,因为这些电话号码可能来自其他应用平台。并且该服务不应允许用户使用已存在的相同电话号码的联系人。我不想为这个快速操作创建自己的联系人选择器。

这是我迄今为止尝试过的。

NSArray<CNPhoneNumber *> *existingPhoneNumbers = @[@"1111", @"2222"];

CNContactPickerViewController *contactPicker = [[CNContactPickerViewController alloc] init];
contactPicker.delegate = self;
contactPicker.displayedPropertyKeys = @[CNContactNamePrefixKey,
                                        CNContactGivenNameKey,
                                        CNContactFamilyNameKey,
                                        CNContactOrganizationNameKey,
                                        CNContactPhoneNumbersKey];

NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"NOT (ANY phoneNumbers.value.stringValue IN %@)", existingPhoneNumbers];

contactPicker.predicateForEnablingContact = predicates;

错误信息:

[CNUI ERROR] Error when showing picker: Error Domain=CNErrorDomain Code=300 "(null)" UserInfo={CNValidationErrors=(
"Error Domain=CNErrorDomain Code=400 \"Invalid Predicate\" UserInfo={CNKeyPaths=(\n    \"phoneNumbers.value.stringValue\"\n), NSLocalizedDescription=Invalid Predicate, NSLocalizedFailureReason=The operation couldn't be completed because its predicate is invalid.}")}

SUBQUERY 仍然无法正常工作。

NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SUBQUERY(phoneNumbers, $CNLabeledValue,$CNLabeledValue.value.stringValue IN %@).@count == 0"];

错误信息:

[CNUI ERROR] Error when showing picker: Error Domain=CNErrorDomain Code=300 "(null)" UserInfo={CNValidationErrors=(
"Error Domain=CNErrorDomain Code=400 \"Invalid Predicate\" UserInfo={CNKeyPaths=(\n    \"value.stringValue\"\n), NSLocalizedDescription=Invalid Predicate, NSLocalizedFailureReason=The operation couldn't be completed because its predicate is invalid.}")}

还尝试使用predicateWithBlock:。它根本不支持基于块的谓词。

来自WWDC 2015 Introducing the Contacts Framework for iOS and OS X session。 他们说predicateForEnablingContact 在“CNContact”上进行评估。该文档没有说谓词必须来自CNContact+Predicates,就像CNContactFetchRequest 一样。

【问题讨论】:

    标签: ios cncontact


    【解决方案1】:

    已经找到问题了。当我遇到这个问题时,我的头发几乎掉光了。解决方案是在 value 中添加撇号。像这样

    let predicate = NSPredicate(format: "ANY self.phoneNumbers.'value'.'digits' BEGINSWITH %@", "+420" 
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      我希望您已经找到了您正在寻找的解决方案。我将分享我是如何处理此类问题的。它可能会帮助其他人。

      我想将 CNContactPickerViewController 限制为仅显示联系人,比如说我在一组中唯一的联系人(电话号码)。因此,基本上,与您的情况不同,我只想显示现有用户。所以,在 viewDidLoad...

      let exsitingNumbers = Set(["1111", "2222"])
      let predicate = NSPredicate(format: "SUBQUERY(phoneNumbers, $x, $x.'value'.'stringValue' IN %@).@count > 0", exsitingNumbers)
      self.contactPicker.predicateForEnablingContact = predicate
      

      For more details about SUBQUERY in NSPredicate,看看吧。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-05
        • 2012-04-26
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 2016-12-26
        相关资源
        最近更新 更多