iosobjective-cexceptioncncontact

Contacts framework raises exception on reading of imageData


I try to fetch imageData after refetching CNContact using unifiedContactWithIdentifier:keysToFetch:error: and CNContactImageDataKey in Objective-C.

I'm using Xcode 7.0 building on iPhone 6 Plus with iOS 9.0.2.

In runtime I always have the same exception CNPropertyNotFetchedException even refetching contact and checking imageData calling imageDataAvailable‌.

Here is the code:

if([currentNativeContact isKeyAvailable:CNContactImageDataKey] &&
    currentNativeContact.imageDataAvailable) {
    previewImage = [UIImage imageWithData:currentNativeContact.thumbnailImageData];
}  else {
    NSError *error;
    currentNativeContact = [self.contactsStore unifiedContactWithIdentifier:currentNativeContact.identifier keysToFetch:@[CNContactImageDataKey] error:&error];
    if(!error && currentNativeContact.imageDataAvailable)
        previewImage = [UIImage imageWithData:currentNativeContact.thumbnailImageData];                            
}

Solution

  • You need to add CNContactImageDataAvailableKey, CNContactImageDataKey and CNContactThumbnailImageDataKey keys to the keysToFetch.