2016-11-03 16 views
1

私はそうのような連絡先ピッカービューを呼び出しています:CNContactPickerViewControllerは、取得エラー(CNPropertyNotFetchedException) - Objective-Cの

- (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 <CNContact: 0x78e56e50: identifier=1861C9BD-143B-4E93-8475-F9079F5D6192, givenName=Kate, familyName=Bell, organizationName=Creative Consulting, phoneNumbers=(not fetched), emailAddresses=(not fetched), postalAddresses=(not fetched)>

これは、シミュレータとiOS9 iPadでのみ発生します。私は私のiOS10 iPhoneでエラーが発生しません。

答えて

0

まず、この許可を求める必要があります。次に、ピッカーを表示することができます。

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) 
    { 

    }