获取iOS上所有联系人的列表


Answers:


98

也许ABPerson函数ABAddressBookCopyArrayOfAllPeople可以做什么?

范例

ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

for ( int i = 0; i < nPeople; i++ )
{
    ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
    ...
}

9
为最近的读者所注意,ABAddressBookCreate()不推荐使用(从iOS 6开始)ABAddressBookCreateWithOptions(NULL, error)
muttley91 2013年

1
@ martin-clayton当有5000多个联系人时如何工作?我们如何管理内存?
萨拉特2015年

@sarat您测量了5000多个联系人消耗了多少内存吗?我不确定这是否是一个问题,至少您在一个循环中分配了太多图像,并且在每次迭代结束前都没有手动取消分配它们。(仅使用autoreleasepool{}
kas-kad

103

在我的原始答案中,在此答案的结尾,我展示了如何以解决此处其他答案所引起的一些问题的方式来检索9.0之前的iOS版本中的联系人。

但是,如果仅支持iOS 9及更高版本,则应使用该Contacts框架,避免使用旧AddressBook框架时出现的一些令人讨厌的桥接问题。

因此,在iOS 9中,您将使用以下Contacts框架:

@import Contacts;

您还需要更新Info.plist,并添加NSContactsUsageDescription来解释为什么您的应用需要访问联系人。

然后执行如下操作:

CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Access to contacts." message:@"This app requires access to contacts because ..." preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"Go to Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:TRUE completion:nil];
    return;
}

CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {

    // make sure the user granted us access

    if (!granted) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // user didn't grant access;
            // so, again, tell user here why app needs permissions in order  to do it's job;
            // this is dispatched to the main queue because this request could be running on background thread
        });
        return;
    }

    // build array of contacts

    NSMutableArray *contacts = [NSMutableArray array];

    NSError *fetchError;
    CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName]]];

    BOOL success = [store enumerateContactsWithFetchRequest:request error:&fetchError usingBlock:^(CNContact *contact, BOOL *stop) {
        [contacts addObject:contact];
    }];
    if (!success) {
        NSLog(@"error = %@", fetchError);
    }

    // you can now do something with the list of contacts, for example, to show the names

    CNContactFormatter *formatter = [[CNContactFormatter alloc] init];

    for (CNContact *contact in contacts) {
        NSString *string = [formatter stringFromContact:contact];
        NSLog(@"contact = %@", string);
    }
}];

如果支持iOS 9.0之前的iOS版本,以下是我的答案。

-

不仅对您的问题,还有此处提供的许多答案(可能无法请求权限,无法ABAddressBookCreateWithOptions正确处理错误或泄漏),有几个反应:

  1. 显然,导入AddressBook框架:

    #import <AddressBook/AddressBook.h>
    

    要么

    @import AddressBook;
    
  2. 您必须请求该应用的权限才能访问联系人。例如:

    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    
    if (status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted) {
        // if you got here, user had previously denied/revoked permission for your
        // app to access the contacts and all you can do is handle this gracefully,
        // perhaps telling the user that they have to go to settings to grant access
        // to contacts
    
        [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        return;
    }
    
    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    
    if (!addressBook) {
        NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
        return;
    }
    
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        if (error) {
            NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
        }
    
        if (granted) {
            // if they gave you permission, then just carry on
    
            [self listPeopleInAddressBook:addressBook];
        } else {
            // however, if they didn't give you permission, handle it gracefully, for example...
    
            dispatch_async(dispatch_get_main_queue(), ^{
                // BTW, this is not on the main thread, so dispatch UI updates back to the main queue
    
                [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
            });
        }
    
        CFRelease(addressBook);
    });
    
  3. 请注意,上面我没有使用其他人建议的模式:

    CFErrorRef *error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    

    那是不对的。正如您将在上面看到的,您想要:

    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    

    前一种模式不能正确捕获错误,而后者可以。如果error不是NULL,请不要忘记CFRelease(或像我一样将所有权转让给ARC),否则您将泄漏该对象。

  4. 要遍历联系人,您需要:

    - (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
    {
        NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
        NSInteger numberOfPeople = [allPeople count];
    
        for (NSInteger i = 0; i < numberOfPeople; i++) {
            ABRecordRef person = (__bridge ABRecordRef)allPeople[i];
    
            NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
            NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
            NSLog(@"Name:%@ %@", firstName, lastName);
    
            ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
    
            CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
            for (CFIndex j = 0; j < numberOfPhoneNumbers; j++) {
                NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, j));
                NSLog(@"  phone:%@", phoneNumber);
            }
    
            CFRelease(phoneNumbers);
    
            NSLog(@"=============================================");
        }
    }
    
  5. 我想提请您注意一个相当关键的细节,即“创建规则”

    Core Foundation函数的名称指示您何时拥有返回的对象:

    • Create名称中嵌入“ ”的对象创建函数;

    • Copy名称中嵌入了“ ”的对象复制功能。

    如果您拥有一个对象,则有责任在完成该对象后放弃所有权(使用CFRelease)。

    这意味着您有责任释放名称为CreateCopy名称的任何Core Foundation函数返回的对象。您可以拨打CFRelease明确的(像我一样上面addressBookphoneNumbers),或者对于对象支持免费桥接,可以将所有权转让给电弧__bridge_transferCFBridgingRelease(如我上面做了allPeoplelastNamefirstName,和phoneNumber)。

    静态分析器(在Xcode中按shift+ command+B或从“产品”菜单中选择“分析”)可以识别许多情况,而您忽略了该“创建规则”而未能释放适当的对象。因此,每当这样编写Core Foundation代码时,请始终通过静态分析器运行它,以确保您没有任何明显的泄漏。


1
注意:有时电话号码中会包含\ U00a0空格。您可以使用phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@“”];删除它们。在获得电话字符串后排队
simon_smiley 2014年

我猜这行中有一个错误'if(status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusDenied){'我想其他状态是CNAuthorizationStatusRestricted
Simon

23

使用此代码显示所有名称+姓氏+电话号码(iOS 6)。也可以在模拟器上工作:

CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
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);

    for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
        NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
        NSLog(@"phone:%@", phoneNumber);
    }

    NSLog(@"=============================================");

}

5
确保您不要忘记先获得许可。这是另一个与此示例一起使用的示例,stackoverflow.com
questions / 12648244 /…

12

确保您输入正确

#import <AddressBook/AddressBook.h>

然后您可以使用以下所有联系人获取CFArray对象

CFArrayRef ABAddressBookCopyArrayOfAllPeople (ABAddressBookRef addressBook);

1
+1是(当前)唯一包含所需库的答案
simon_smiley 2014年

10

在iOS 6中,请确保使用ABAddressBookCreateWithOptions,这是的更新版本ABAddressBookCreate

CFErrorRef * error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

for(int i = 0; i < numberOfPeople; i++){
  ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
  // More code here
}

1
效果很好,文档解释了如何按字母顺序对其进行排序:AddressBookProgrammingGuideforiPhone
Patrick Patrick

@Patrick会按字母顺序对姓氏进行排序,是否知道如何对名字进行处理?
Sheharyar 2014年

@Patrick请检查此问题并帮助我解决。stackoverflow.com/q/47506554/2910061
Ilesh P

9

更新iOS 9.0。苹果已经弃用AddressBook,现在他们添加了Contacts框架:

添加CNContactStore属性并按如下方式定义它:

self.contactsStrore = [[CNContactStore alloc] init];

然后添加以下方法以读取所有联系人:

-(void)checkContactsAccess{

[self requestContactsAccessWithHandler:^(BOOL grandted) {

    if (grandted) {

        CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactNamePrefixKey, CNContactMiddleNameKey, CNContactPhoneNumbersKey]];
        [self.contactsStrore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {

            NSLog(@"%@", contact.familyName);
            NSLog(@"%@", contact.givenName);
            NSLog(@"%@", contact.namePrefix);
            NSLog(@"%@", contact.middleName);
            NSLog(@"%@", contact.phoneNumbers);
            NSLog(@"=============================================");
        }];
    }
}];
}

-(void)requestContactsAccessWithHandler:(void (^)(BOOL grandted))handler{

switch ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]) {
    case CNAuthorizationStatusAuthorized:
        handler(YES);
        break;
    case CNAuthorizationStatusDenied:
    case CNAuthorizationStatusNotDetermined:{
        [self.contactsStrore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {

            handler(granted);
        }];
        break;
    }
    case CNAuthorizationStatusRestricted:
        handler(NO);
        break;
}
}

在iOS 9.0之前=>使用AddressBook框架。您必须先检查访问权限并请求访问用户联系人:

// Prompt the user for access to their Address Book data
-(void)requestAddressBookAccess
{
   YourViewController * __weak weakSelf = self;

  ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error)
                                         {
                                             if (granted)
                                             {
                                                 dispatch_async(dispatch_get_main_queue(), ^{
                                                     [weakSelf accessGrantedForAddressBook];

                                                 });
                                             }
                                         });
}



-(void)checkAddressBookAccess
{
   switch (ABAddressBookGetAuthorizationStatus())
       {
        // Update our UI if the user has granted access to their Contacts
    case  kABAuthorizationStatusAuthorized:
        [self accessGrantedForAddressBook];
        break;
        // Prompt the user for access to Contacts if there is no definitive answer
    case  kABAuthorizationStatusNotDetermined :
        [self requestAddressBookAccess];
        break;
        // Display a message if the user has denied or restricted access to Contacts
    case  kABAuthorizationStatusDenied:
    case  kABAuthorizationStatusRestricted:
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Warning"
                                                        message:@"Permission was not granted for Contacts."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
        break;
    default:
        break;
 }
   }

7

感谢mahesh和wzbozon,以下代码对我有用:

CFErrorRef * error = NULL;
addressBook = ABAddressBookCreateWithOptions(NULL, error);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
 {
     if (granted)
     {
         dispatch_async(dispatch_get_main_queue(), ^{
             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);

                 NSMutableArray *numbers = [NSMutableArray array];
                 for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
                     NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
                     [numbers addObject:phoneNumber];
                 }

                 NSMutableDictionary *contact = [NSMutableDictionary dictionary];
                 [contact setObject:name forKey:@"name"];
                 [contact setObject:numbers forKey:@"numbers"];

                 [all_contacts addObject:contact];
             }
         });
     }
 });

5

迅捷版:

override func viewDidLoad() {
    super.viewDidLoad()

    var error: Unmanaged<CFErrorRef>?

    var addressBook: ABAddressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()


    if ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.NotDetermined {
        ABAddressBookRequestAccessWithCompletion(addressBook,  {

            (granted:Bool, error:CFErrorRef!) -> Void in

            self.populateFrom(addressBook: addressBook)

        })
    }
    else if ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.Authorized {

        self.populateFrom(addressBook: addressBook)

    }

}

func populateFrom(#addressBook:ABAddressBook){
    let allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue()
    let nPeople = ABAddressBookGetPersonCount(addressBook)
    for index in 0..<nPeople{
        let person: ABRecordRef = Unmanaged<ABRecordRef>.fromOpaque(COpaquePointer(CFArrayGetValueAtIndex(allPeople, index))).takeUnretainedValue()
        let firstName: String = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeUnretainedValue() as? String
        println("\(firstName.debugDescription)")


    }

}


1

这适用于ios 7和ios 8,希望它对您有帮助。

NSMutableArray *result = [[NSMutableArray alloc] init];
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
__block BOOL accessGranted = NO;

if (ABAddressBookRequestAccessWithCompletion != NULL){
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else{
    accessGranted = YES;
}
if (accessGranted){
    // If the app is authorized to access the first time then add the contact
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

    for (int i=0; i<numberOfPeople; i++){
        CFStringRef phone;
        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
        CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
        CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
        NSString *userName = @"NoName";

        userName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
        userName = [userName stringByReplacingOccurrencesOfString:@"(null)" withString:@""];

        ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
        CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers );

        phone = nil;

        for ( CFIndex ind= 0; ind<phoneNumberCount; ind++ ){
            CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, ind);
            CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, ind);

            // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"
            // Find the ones you want here
            if (phoneNumberLabel != nil){
                NSStringCompareOptions  compareOptions = NSCaseInsensitiveSearch;
                if(CFStringCompare(phoneNumberLabel, CFSTR("mobile"),compareOptions)){
                    phone = phoneNumberValue;
                }
                phone = phoneNumberValue;

                NSStringCompareOptions  compareOptionss = NSCaseInsensitiveSearch;
                if(!CFStringCompare(phone, CFSTR("1-800-MY-APPLE"),compareOptionss)){
                    continue;
                }
                NSMutableArray *theKeys = [NSMutableArray arrayWithObjects:@"name", @"small_name",@"phone", @"checked", nil];
                NSMutableArray *theObjects = [NSMutableArray arrayWithObjects:userName, [userName lowercaseString],phone, @"NO", nil];
                NSMutableDictionary *theDict = [NSMutableDictionary dictionaryWithObjects:theObjects forKeys:theKeys];
                if (![[functions formatNumber:(__bridge NSString *)(phone)] isEqualToString:[[NSUserDefaults standardUserDefaults]valueForKey:@"phoneNumber"]]){
                    [result addObject:theDict];
                }
            }
        }
    }
}
//sort array
NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"small_name"
                                                            ascending:YES]; // 1
NSArray * sortedArray = [result sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

functions最后if一种情况是不确定的。也CFStringCompare尝试转换NSStringCompareOptionsNSStringCompareFlags
猛禽2015年

0
ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
  ...
}

0

这是一个完整的演示,用于获取具有表视图的所有联系人。

import UIKit
import ContactsUI
import AddressBook
import Contacts

class ShowContactsVC: UIViewController,CNContactPickerDelegate,UITableViewDelegate,UITableViewDataSource
{



@IBOutlet weak var tableView: UITableView!
let peoplePicker = CNContactPickerViewController()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var contacts = [CNContact]()
var option : Int = 0
var userAccessGranted : Bool = false
var dataArray : NSMutableArray?




override func viewDidLoad()
{
    super.viewDidLoad()

    peoplePicker.delegate = self

    self.checkIfUserAccessGranted()

    self.tableView.delegate = self
    self.tableView.dataSource = self


    navigationController!.navigationBar.barTintColor = UIColor.grayColor()


              if(self.userAccessGranted)
      {
         self.tableView.hidden = false
        fetchContacts()

    }

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if dataArray == nil {
        return 0;
    }
    else{


    return dataArray!.count
}
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("TableCell", forIndexPath: indexPath) as! ContactsTableViewCell



    let data = dataArray![indexPath.row] as! Data;
    cell.lblName.text = data.name
    cell.imgContact.image = data.image
    return cell
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
    cell.backgroundColor = UIColor.cyanColor()

}




func checkIfUserAccessGranted()
{
    appDelegate.requestForAccess { (accessGranted) -> Void in
        if accessGranted {
            self.userAccessGranted = true;
        }else{
            self.userAccessGranted = false;
        }
    }
}





func fetchContacts()
{

    dataArray = NSMutableArray()

    let toFetch = [CNContactGivenNameKey, CNContactImageDataKey, CNContactFamilyNameKey, CNContactImageDataAvailableKey]
    let request = CNContactFetchRequest(keysToFetch: toFetch)

    do{
        try appDelegate.contactStore.enumerateContactsWithFetchRequest(request) {
            contact, stop in
            print(contact.givenName)
            print(contact.familyName)
            print(contact.identifier)

            var userImage : UIImage;
            // See if we can get image data
            if let imageData = contact.imageData {
                //If so create the image
                userImage = UIImage(data: imageData)!
            }else{
                userImage = UIImage(named: "no_contact_image")!
            }

            let data = Data(name: contact.givenName, image: userImage)
            self.dataArray?.addObject(data)

        }
    } catch let err{
        print(err)

    }

    self.tableView.reloadData()

}

func contactPickerDidCancel(picker: CNContactPickerViewController)
{
    picker.dismissViewControllerAnimated(true, completion: nil)
    self.navigationController?.popToRootViewControllerAnimated(true)
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



}




import UIKit

class Data {


    let name : String
    let image : UIImage

    init(name : String, image : UIImage) {
        self.image = image
        self.name = name
    }

}

此代码是否也用于从联系人生成QR代码?
Suhail

0

如果要按字母顺序排序,可以使用以下代码。

 CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault,
                                                           CFArrayGetCount(allPeople),
                                                           allPeople);

CFArraySortValues(peopleMutable,
                  CFRangeMake(0, CFArrayGetCount(peopleMutable)),
                  (CFComparatorFunction) ABPersonComparePeopleByName,
                  kABPersonSortByFirstName);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.