CFErrorRef *error = NulL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NulL,error); //ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef allPeople = ABAddressBookcopyArrayOfAllPeople(addressBook); CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook); for(int i = 0; i < numberOfPeople; i++) { ABRecordRef person = CFArrayGetValueAtIndex( allPeople,i ); Nsstring *firstname = (__brIDge Nsstring *)(ABRecordcopyValue(person,kABPersonFirstnameProperty)); Nsstring *lastname = (__brIDge Nsstring *)(ABRecordcopyValue(person,kABPersonLastnameProperty)); // NSLog(@"name:%@ %@",firstname,lastname); ABMultiValueRef phoneNumbers = ABRecordcopyValue(person,kABPersonPhoneProperty); Nsstring *phoneNumber; for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) { phoneNumber = (__brIDge_transfer Nsstring *) ABMultiValuecopyValueAtIndex(phoneNumbers,i); // NSLog(@"phone:%@",phoneNumber); }解决方法 今天更新了我的例子和删除内存泄漏:)
+ (NSArray *)getAllContacts { CFErrorRef *error = nil; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NulL,error); ABRecordRef source = ABAddressBookcopyDefaultSource(addressBook); CFArrayRef allPeople = (ABAddressBookcopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook,source,kABPersonSortByFirstname)); //CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); CFIndex nPeople = CFArrayGetCount(allPeople); // BUGfix who synced contacts with facebook NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople]; if (!allPeople || !nPeople) { NSLog(@"people nil"); } for (int i = 0; i < nPeople; i++) { @autoreleasepool { //data model ContactsData *contacts = [ContactsData new]; ABRecordRef person = CFArrayGetValueAtIndex(allPeople,i); //get First name CFStringRef firstname = (CFStringRef)ABRecordcopyValue(person,kABPersonFirstnameProperty); contacts.firstnames = [(__brIDge Nsstring*)firstname copy]; if (firstname != NulL) { CFRelease(firstname); } //get Last name CFStringRef lastname = (CFStringRef)ABRecordcopyValue(person,kABPersonLastnameProperty); contacts.lastnames = [(__brIDge Nsstring*)lastname copy]; if (lastname != NulL) { CFRelease(lastname); } if (!contacts.firstnames) { contacts.firstnames = @""; } if (!contacts.lastnames) { contacts.lastnames = @""; } contacts.contactID = ABRecordGetRecordID(person); //append first name and last name contacts.fullname = [Nsstring stringWithFormat:@"%@ %@",contacts.firstnames,contacts.lastnames]; // get contacts picture,if pic doesn't exists,show standart one CFDataRef imgData = ABPersoncopyImageData(person); NSData *imageData = (__brIDge NSData *)imgData; contacts.image = [UIImage imageWithData:imageData]; if (imgData != NulL) { CFRelease(imgData); } if (!contacts.image) { contacts.image = [UIImage imagenamed:@"avatar.png"]; } //get Phone Numbers NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init]; ABMultiValueRef multiPhones = ABRecordcopyValue(person,kABPersonPhoneProperty); for(CFIndex i=0; i<ABMultiValueGetCount(multiPhones); i++) { @autoreleasepool { CFStringRef phoneNumberRef = ABMultiValuecopyValueAtIndex(multiPhones,i); Nsstring *phoneNumber = CFBrIDgingrelease(phoneNumberRef); if (phoneNumber != nil)[phoneNumbers addobject:phoneNumber]; //NSLog(@"All numbers %@",phoneNumbers); } } if (multiPhones != NulL) { CFRelease(multiPhones); } [contacts setNumbers:phoneNumbers]; //get Contact email NSMutableArray *contactEmails = [NSMutableArray new]; ABMultiValueRef multIEmails = ABRecordcopyValue(person,kABPersonEmailProperty); for (CFIndex i=0; i<ABMultiValueGetCount(multIEmails); i++) { @autoreleasepool { CFStringRef contactEmailRef = ABMultiValuecopyValueAtIndex(multIEmails,i); Nsstring *contactEmail = CFBrIDgingrelease(contactEmailRef); if (contactEmail != nil)[contactEmails addobject:contactEmail]; // NSLog(@"All emails are:%@",contactEmails); } } if (multIEmails != NulL) { CFRelease(multIEmails); } [contacts setEmails:contactEmails]; [items addobject:contacts];#ifdef DEBUG //NSLog(@"Person is: %@",contacts.firstnames); //NSLog(@"Phones are: %@",contacts.numbers); //NSLog(@"Email is:%@",contacts.emails);#endif } } //autoreleasepool CFRelease(allPeople); CFRelease(addressBook); CFRelease(source); return items;}总结
以上是内存溢出为你收集整理的iphone – 在iOS 7中获取联系人全部内容,希望文章能够帮你解决iphone – 在iOS 7中获取联系人所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)