【问题标题】:CNContactPickerViewController gets error (CNPropertyNotFetchedException) - Objective-CCNContactPickerViewController 出现错误(CNPropertyNotFetchedException) - Objective-C
【发布时间】:2016-11-03 14:50:03
【问题描述】:

我这样调用联系人选择器视图:

- (void)openContacts {
    CNContactPickerViewController *picker = [[CNContactPickerViewController alloc] init];

    picker.delegate = self;

    picker.displayedPropertyKeys = @[CNContactGivenNameKey];

    [self presentViewController:picker animated:YES completion:nil];
}

我正在使用这个委托方法处理选择:

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts {
    for (CNContact *contact in contacts) {
        NSLog(@"%@",contact);
        NSString *phone;
        NSString *mobilePhone;

        for (CNLabeledValue *label in contact.phoneNumbers) {
            if ([label.label isEqualToString:CNLabelPhoneNumberMobile] || [label.label isEqualToString:CNLabelPhoneNumberiPhone]) {
                mobilePhone = [label.value stringValue];
            } else if ([label.label isEqualToString:CNLabelPhoneNumberMain]) {
                phone = [label.value stringValue];
            }
        }
    }
}

NSLog 为联系人输出以下内容:&lt;CNContact: 0x78e56e50: identifier=1861C9BD-143B-4E93-8475-F9079F5D6192, givenName=Kate, familyName=Bell, organizationName=Creative Consulting, phoneNumbers=(not fetched), emailAddresses=(not fetched), postalAddresses=(not fetched)&gt;

这只发生在模拟器和 iOS9 iPad 上。我的 iOS10 iPhone 没有出现错误。

【问题讨论】:

    标签: objective-c ios9 cncontact


    【解决方案1】:

    您应该首先获得许可。然后就可以显示picker了。

    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error)
     {
             CNContactPickerViewController *picker = [[CNContactPickerViewController alloc] init];
             picker.delegate = self;
             picker.displayedPropertyKeys = @[CNContactGivenNameKey];
             [self presentViewController:picker animated:YES completion:nil];
     }];
    

    在这种情况下,您将始终显示选择器,而不依赖于用户的选择。所以你应该像这样检查房产的可用性:

    if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized)
        {
    
        }
    

    【讨论】:

      猜你喜欢
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多