Unable to create person using VCard view

I am developing an application using Xcode 4.2 and I am trying to create ABPerson using initWithVCardRepresentation and / or ABPersonCreatePeopleInSourceWithVCardRepresentation, but I can not find a working example. can someone help?

I get VCard in NSString format ....

thank

+2
source share
4 answers

This is a complete example, and it works great, it is based on the latest version of iOS 8.

First of all, you should check the authorization status and access rights, if not, and then save the vcard, just look at the code below:

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
        NSLog(@"Authorized");
        [self addVcardsIntoAddressBook:vcard];
    } else{
        NSLog(@"Not determined");
        ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
            if (!granted){
                NSLog(@"Just denied");
                return;
            }
            NSLog(@"Just authorized");
            [self addVcardsIntoAddressBook:vcard];
        });
    }

Here's how to add vcard:

- (void)addVcardsIntoAddressBook:(NSData *)vcard {
    CFDataRef vCardData = CFDataCreate(NULL, [vcard bytes], [vcard length]);
    ABAddressBookRef book = ABAddressBookCreate();
    ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);
    CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
    for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
        ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
        ABAddressBookAddRecord(book, person, NULL);
        CFRelease(person);
    }
    ABAddressBookSave(book, NULL);

}

+2

Swift acoustic;

        let vCard : NSData // vcard

        let addressBook: ABAddressBook? = ABAddressBookCreateWithOptions(nil, nil)?.takeRetainedValue()   

                ABAddressBookRequestAccessWithCompletion(addressBook) {
                        granted, error in

                        if !granted {
                            return
                        }

                    let vCardData = CFDataCreate(nil, UnsafePointer<UInt8>(vCard.bytes), vCard.length)
                    let defaultSource  = ABAddressBookCopyDefaultSource(addressBook)
                    let vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource.takeUnretainedValue(), vCardData).takeRetainedValue() as NSArray

                    for person in vCardPeople {

                        ABAddressBookAddRecord(addressBook, person, nil)

                    }

                    let isSaved = ABAddressBookSave(addressBook, nil)
                    if isSaved{
                        //succesfully saved
                    }
                    else{
                       //not saved
                    }
        }
+2

, - :

// Assuming your vCard is stored in vCardString as an NSString
CFDataRef vCardData = (__bridge CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding];
ABAddressBookRef book = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
CFIndex index = 0;
ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
+1

, Vcard 2.1 3.0

NSString * filename = [[NSBundle mainBundle] pathForResource: @ "Contacts" ofType: @ "vcf" ];   NSLog (@ " % @", );   NSData * stringData = [NSData dataWithContentsOfFile: _];   NSString * vCardString = [[NSString alloc] initWithData: stringData encoding: NSUTF8StringEncoding];

CFDataRef vCardData = (__bridge CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding];

ABAddressBookRef book = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
    ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
    ABAddressBookAddRecord(book, person, NULL);
}

CFRelease(vCardPeople);
CFRelease(defaultSource);
ABAddressBookSave(book, NULL);
CFRelease(book);
0

All Articles