【我就看看不说话】读取通讯录

2024-08-27 07:58
文章标签 读取 通讯录 看看 说话

本文主要是介绍【我就看看不说话】读取通讯录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


一、在工程中添加AddressBook.frameworkAddressBookUI.framework


二、获取通讯录


1、在infterface中定义数组并在init方法中初始化


NSMutableArray *addressBookTemp;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    addressBookTemp = [NSMutableArray array];

}

2、定义一个model,用来存放通讯录中的各个属性

新建一个继承自NSObject的类,在.h



@interface TKAddressBook : NSObject {

    NSInteger sectionNumber;

    NSInteger recordID;

    NSString *name;

    NSString *email;

    NSString *tel;

}

@property NSInteger sectionNumber;

@property NSInteger recordID;

@property (nonatomic, retain) NSString *name;

@property (nonatomic, retain) NSString *email;

@property (nonatomic, retain) NSString *tel;


@end

.m文件中进行synthesize


@implementation TKAddressBook

@synthesize name, email, tel, recordID, sectionNumber;


@end

3、获取联系人


iOS6之后,获取通讯录需要获得权限



//新建一个通讯录类

ABAddressBookRef addressBooks = nil;


if([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)


{

    addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);

    

    //获取通讯录权限

    

    dispatch_semaphore_t sema = dispatch_semaphore_create(0);

    

    ABAddressBookRequestAccessWithCompletion(addressBooks, ^(boolgranted, CFErrorRef error){dispatch_semaphore_signal(sema);});

    

    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

    

    dispatch_release(sema);

    

}


else


{

    addressBooks = ABAddressBookCreate();

    

}


//获取通讯录中的所有人

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);



//通讯录中人数

CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);


//循环,获取每个人的个人信息

for(NSInteger i = 0; i < nPeople; i++)

{

    //新建一个addressBook model

    TKAddressBook *addressBook = [[TKAddressBook alloc] init];

    //获取个人

    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

    //获取个人名字

    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];

        }

    }

    addressBook.name = nameString;

    addressBook.recordID = (int)ABRecordGetRecordID(person);;

    

    ABPropertyID multiProperties[] = {

        kABPersonPhoneProperty,

        kABPersonEmailProperty

    };

    NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);

    for(NSInteger j = 0; j < multiPropertiesTotal; j++) {

        ABPropertyID property = multiProperties[j];

        ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);

        NSInteger valuesCount = 0;

        if(valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);

        

        if(valuesCount == 0) {

            CFRelease(valuesRef);

            continue;

        }

        //获取电话号码和email

        for(NSInteger k = 0; k < valuesCount; k++) {

            CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);

            switch(j) {

                case0: {// Phone number

                    addressBook.tel = (__bridge NSString*)value;

                    break;

                }

                case1: {// Email

                    addressBook.email = (__bridge NSString*)value;

                    break;

                }

            }

            CFRelease(value);

        }

        CFRelease(valuesRef);

    }

    //将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息

    [addressBookTemp addObject:addressBook];

    

    if(abName) CFRelease(abName);

        if(abLastName) CFRelease(abLastName);

            if(abFullName) CFRelease(abFullName);

                }

三、显示在table


//行数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return1;

}


//列数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return[addressBookTemp count];

}


//cell内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    NSString *cellIdentifier = @"ContactCell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    

    if(cell == nil){

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    }

    

    TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row];

    

    cell.textLabel.text = book.name;

    

    cell.detailTextLabel.text = book.tel;

    

    returncell;

}

列表效果



这篇关于【我就看看不说话】读取通讯录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1111090

相关文章

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

C#实现SHP文件读取与地图显示的完整教程

《C#实现SHP文件读取与地图显示的完整教程》在地理信息系统(GIS)开发中,SHP文件是一种常见的矢量数据格式,本文将详细介绍如何使用C#读取SHP文件并实现地图显示功能,包括坐标转换、图形渲染、平... 目录概述功能特点核心代码解析1. 文件读取与初始化2. 坐标转换3. 图形绘制4. 地图交互功能缩放

java读取excel文件为base64实现方式

《java读取excel文件为base64实现方式》文章介绍使用ApachePOI和EasyExcel处理Excel文件并转换为Base64的方法,强调EasyExcel适合大文件且内存占用低,需注意... 目录使用 Apache POI 读取 Excel 并转换为 Base64使用 EasyExcel 处

使用Java读取本地文件并转换为MultipartFile对象的方法

《使用Java读取本地文件并转换为MultipartFile对象的方法》在许多JavaWeb应用中,我们经常会遇到将本地文件上传至服务器或其他系统的需求,在这种场景下,MultipartFile对象非... 目录1. 基本需求2. 自定义 MultipartFile 类3. 实现代码4. 代码解析5. 自定

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3

Python使用openpyxl读取Excel的操作详解

《Python使用openpyxl读取Excel的操作详解》本文介绍了使用Python的openpyxl库进行Excel文件的创建、读写、数据操作、工作簿与工作表管理,包括创建工作簿、加载工作簿、操作... 目录1 概述1.1 图示1.2 安装第三方库2 工作簿 workbook2.1 创建:Workboo

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati