有没有办法在Mac上将地址簿联系人添加到iOS 8.1模拟器。在谷歌上找到的所有答案都是旧的,不再有效。我找到的只有一个链接是Importing AddressBook data into the iPhone Simulator
如果有人这样做,请提供更新的答案。旧的解决方案不起作用,因为Apple改变了iOS 8中的所有结构
答案 0 :(得分:3)
使用iOS 8.1
在iPhone6+ Simulator
和XCode 6.1
上进行了测试。
我制作了两个方法addSampleContacts
和removeSampleContacts
,您可以在项目中添加这些方法,以便在模拟器中添加(或删除)联系人。
为此,
AddressBook.framework
和AddressBookUI.framework
。#import <AddressBook/AddressBook.h>
.vcf
格式)。您可以使用手机的联系人文件进行此测试,以获得大量实际(真实)联系人。在项目中添加该文件。-(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.");
}
如果您之前已经这样做过,可能不需要询问(或检查)许可。