美文网首页iOS奋斗iOS Developer编程知识点
iOS获取通讯录电话号码与姓名

iOS获取通讯录电话号码与姓名

作者: 凯文Kevin21 | 来源:发表于2017-03-08 16:49 被阅读1340次
    • 本文主要写iOS如何访问通讯录,获取联系人的姓名、电话号码、头像等信息。

    1. 导入#import <AddressBook/AddressBook.h>框架。
    2. 定义数组@property (nonatomic, strong)NSMutableArray *dataArray;并且初始化。
    3. 建立模型UserModel 包括以下属性:
    @property (nonatomic,strong)NSString  *name;   //姓名
    @property(nonatomic, strong) NSArray *phoneNumbers;    //电话号码
    @property (nonatomic,strong)NSString *email;    //邮箱
    @property (nonatomic,strong)NSData *imgStr;    //头像
    @property (nonatomic, assign)ABRecordID  recordID;
    
    1. 下面附上实现的所有代码:
    -(void)getPersonData{
        if (self.dataArray == nil) {
            self.dataArray = [NSMutableArray array];
    }
     ABAddressBookRef adressBook = ABAddressBookCreateWithOptions(NULL, NULL);
         if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
            CFErrorRef error = NULL;
            adressBook = ABAddressBookCreateWithOptions(NULL, &error);
            //创建一个初始信号量为0的信号
            dispatch_semaphore_t sema = dispatch_semaphore_create(0);
            //申请访问权限
              ABAddressBookRequestAccessWithCompletion(adressBook, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});
            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        }else{
            adressBook = ABAddressBookCreate();
        }
        //获取所有联系人的数组
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(adressBook);
        //遍历所有的联系人
        CFIndex peopleCount = CFArrayGetCount(allPeople);
        //如果没有联系人
        if (!peopleCount) {
            NSLog(@"没有任何联系人");
            return;
        }
        
        NSMutableArray *contactsListArrayM = [NSMutableArray array];
        
        for (NSInteger index=0; index < peopleCount; index++) {
            UserModel *personModel = [[UserModel alloc]init];
            NSMutableArray *phoneArrayM = [NSMutableArray array];
            
            //姓名
            ABRecordRef person = CFArrayGetValueAtIndex(allPeople, index);
            //获取个人名字
            CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
            
            CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
            CFStringRef abFullName = ABRecordCopyCompositeName(person);
            NSString *nameString = (__bridge NSString *)abName;
            NSString *lastNameString = (__bridge NSString *)abLastName;
    
            if ((__bridge id)abFullName !=nil) {
                nameString = (__bridge NSString *)abFullName;
            }else{
                if ((__bridge id)abLastName != nil) {
                    nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];
                }
            }
            personModel.name = nameString;
            
            ABRecordID  recordIDs = ABRecordGetRecordID(person);
            //获取联系人电话(一个联系人可能存在多个电话)
            ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
            CFIndex phoneCount = ABMultiValueGetCount(phones);
            
            for (NSInteger index = 0; index < phoneCount; index++) {
                NSString *phoneValue = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, index));
                [phoneArrayM addObject:phoneValue];
            }
            NSArray *phonesNumber = [NSArray arrayWithArray:phoneArrayM];
            personModel.phoneNumbers = phonesNumber;
            
            personModel.imgStr = (__bridge NSData*)ABPersonCopyImageData(person);
            personModel.recordID = recordIDs;
            
            [contactsListArrayM addObject:personModel];
            CFRelease(phones);
        }
        CFRelease(adressBook);
        CFRelease(allPeople);
        //C语言部分需要手动释放内存
    
        self.dataArray = [NSMutableArray arrayWithArray:contactsListArrayM];
        
        for (UserModel *model  in self.dataArray) {
            NSLog(@"name:%@ ---%@",model.name, model.phoneNumbers[0]);
        }  
    }
    

    相关文章

      网友评论

      本文标题:iOS获取通讯录电话号码与姓名

      本文链接:https://www.haomeiwen.com/subject/eagbgttx.html