Pregunta

I've found a code to save a vcard to the device. This code works perfect on the simulator, but when trying the code on a device, I don't see the new contact. The code is:

NSString *vCardString = @"BEGIN:VCARD\nVERSION:2.1\nN:Standard11.3,Joe;\nADR:PO Box 555,Suite 55,5555 Any Street,San Diego,CA,92111,USA;;\nEND:VCARD";

    CFDataRef vCardData = (CFDataRef)CFBridgingRetain([vCardString dataUsingEncoding:NSUTF8StringEncoding]);  ABAddressBookRef book = ABAddressBookCreate();
    ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);
    CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
    CFRelease(vCardData);
    for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
        ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
        ABAddressBookAddRecord(book, person, NULL);
        //CFRelease(person);
    }
    CFRelease(vCardPeople);
    CFRelease(defaultSource);
    ABAddressBookSave(book, NULL);
    CFRelease(book);

Any idea why? Another thing, is that I saw in other app on the settings permission to use the address book, maybe thats the solution?

¿Fue útil?

Solución

If this is on iOS 6, you need to ask the user's permission to get access to the address book.

ABAddressBookRef ref = ABAddressBookCreate();
ABAddressBookRequestAccessWithCompletion(ref, ^(bool granted, CFErrorRef error) {
   if(granted) {
      //Use the address book
   }
});

This will bring up the prompt so the user can approve your app.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top