如何在模拟器iOS 8中添加地址簿联系人

时间:2015-01-07 07:56:10

标签: ios-simulator contacts ios8.1

有没有办法在Mac上将地址簿联系人添加到iOS 8.1模拟器。在谷歌上找到的所有答案都是旧的,不再有效。我找到的只有一个链接是Importing AddressBook data into the iPhone Simulator

如果有人这样做,请提供更新的答案。旧的解决方案不起作用,因为Apple改变了iOS 8中的所有结构

1 个答案:

答案 0 :(得分:3)

使用iOS 8.1iPhone6+ SimulatorXCode 6.1上进行了测试。

我制作了两个方法addSampleContactsremoveSampleContacts,您可以在项目中添加这些方法,以便在模拟器中添加(或删除)联系人。

为此,

  1. 您需要在项目中添加AddressBook.frameworkAddressBookUI.framework
  2. 在要添加/删除示例联系人的类(或控制器)中导入#import <AddressBook/AddressBook.h>
  3. 您需要一个联系人文件(.vcf格式)。您可以使用手机的联系人文件进行此测试,以获得大量实际(真实)联系人。在项目中添加该文件。
  4. 如果您已经设置了这个,请添加以下功能。

  5. -(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.");
    }
    

    如果您之前已经这样做过,可能不需要询问(或检查)许可。