Suraj K Thomas
Suraj K Thomas

Reputation: 5883

How to add address book contacts in simulator iOS 8

Is there any way to add address book contacts on your mac to iOS 8.1 simulator. All the answers found on google are old and no longer works. Only one link I found was Importing AddressBook data into the iPhone Simulator

Please provide an updated answer if some one has done this. The old solution don't work because Apple have changed all the structure in iOS 8

Upvotes: 2

Views: 5782

Answers (1)

Hemang
Hemang

Reputation: 27072

Tested on iOS 8.1 and iPhone6+ Simulator with XCode 6.1.

I made two methods addSampleContacts and removeSampleContacts which you can add in your project to add (or remove) contacts in your simulator.

For that,

  1. You need to add AddressBook.framework and AddressBookUI.framework in your project.
  2. Import #import <AddressBook/AddressBook.h> in the class (or controller) where you want to add/remove sample contacts
  3. You need a contact file (of .vcf format). You can use your phone's contact file for this test to have large number of actual (real) contacts. Add that file in your project.
  4. if you've setup this then, add below functions for the same.

-(void)addSampleContacts
{
    NSError *error;
    CFErrorRef castError = (__bridge CFErrorRef)error;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &castError);
    __block BOOL accessAllowed = NO;

    if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6 or above
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);

        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            accessAllowed = granted;
            dispatch_semaphore_signal(sema);
        });

        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }

    if(accessAllowed)
    {
        NSString *vFilePath = [[NSBundle mainBundle] pathForResource:@"YourContactFile" ofType:@"vcf"];
        NSData *myData = [NSData dataWithContentsOfFile:vFilePath];
        CFDataRef vCardData = (__bridge CFDataRef)myData;

        NSError *error;
        CFErrorRef castError = (__bridge CFErrorRef)error;
        ABAddressBookRef ContactBook = ABAddressBookCreateWithOptions(NULL, &castError);
        ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(ContactBook);
        CFArrayRef vCardContact = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
        NSArray *arrayContacts = (__bridge_transfer NSArray *)vCardContact;
        NSInteger totalVCFContactCount = arrayContacts.count;

        for (CFIndex index = 0; index < totalVCFContactCount; index++)
        {
            ABRecordRef contact = CFArrayGetValueAtIndex(vCardContact, index);
            ABAddressBookAddRecord(ContactBook, contact, NULL);
            ABAddressBookSave(ContactBook, nil);
            CFRelease(contact);
        }

        CFRelease(vCardContact);
        CFRelease(defaultSource);
    }


    NSLog(@"Contacts added.");
}

-(void)removeSampleContacts
{
    NSError *error;
    CFErrorRef castError = (__bridge CFErrorRef)error;
    ABAddressBookRef contactBook = ABAddressBookCreateWithOptions(NULL, &castError);

    __block BOOL accessAllowed = NO;

    if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6 or above
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);

        ABAddressBookRequestAccessWithCompletion(contactBook, ^(bool granted, CFErrorRef error) {
            accessAllowed = granted;
            dispatch_semaphore_signal(sema);
        });

        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }

    if(accessAllowed)
    {
        CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople( contactBook );
        NSArray *arrayContacts = (__bridge_transfer NSArray *)allContacts;

        for ( int i = 0; i < arrayContacts.count; i++ )
        {
            ABRecordRef ref = CFArrayGetValueAtIndex(allContacts, i);
            ABAddressBookRemoveRecord(contactBook, ref, nil);
            ABAddressBookSave(contactBook, nil);
        }
    }

    NSLog(@"Contacts removed.");
}

You may not need to ask (or check) for permission if you're already doing this before.

Upvotes: 3

Related Questions