2016-11-03 8 views
1

Ich mag so den Kontakt Picker Ansicht nenne:CNContactPickerViewController bekommt Fehler (CNPropertyNotFetchedException) - Objective-C

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

    picker.delegate = self; 

    picker.displayedPropertyKeys = @[CNContactGivenNameKey]; 

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

Und ich mit diesem Delegatmethode die Auswahl am Umgang:

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

Die NSLog gibt folgende für Kontakt aus: <CNContact: 0x78e56e50: identifier=1861C9BD-143B-4E93-8475-F9079F5D6192, givenName=Kate, familyName=Bell, organizationName=Creative Consulting, phoneNumbers=(not fetched), emailAddresses=(not fetched), postalAddresses=(not fetched)>

Dies tritt nur auf Simulator und iOS9 iPad. Ich bekomme keinen Fehler mit meinem iOS10 iPhone.

Antwort

0

Sie sollten zuerst um Erlaubnis fragen. Dann können Sie den Picker zeigen.

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]; 
}]; 

In diesem Fall Sie Picker immer zeigen werden, ohne Abhängigkeit von der Wahl des Benutzers. Also sollten Sie die Verfügbarkeit der Immobilie so überprüfen:

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

    } 
Verwandte Themen