ios 读取通讯录

1.获取通讯录列表

+(NSMutableDictionary*)getAddressPeopleArray
{
    BOOL granted = [AddressEngine getAccessGranted];
//    NSMutableArray*addArray=[NSMutableArray arrayWithCapacity:0];
    NSMutableDictionary * addrDic = [NSMutableDictionary dictionaryWithCapacity:0];
    if (granted)
    {
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        });
        NSArray *array = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
        // NSLog(@"kaishi");
        for (int i = 0; i<array.count;i++)
        {
            ABRecordRef person = (__bridge ABRecordRef)([array objectAtIndex:i]);
            ABMutableMultiValueRef ref = ABRecordCopyValue(person, kABPersonPhoneProperty);//读取电话 格式111-2222-2222
            addressPeople * people=[[addressPeople alloc]init];
            NSInteger nCount = ABMultiValueGetCount(ref);//单个人电话数量
            if ( nCount>5 || nCount<1 )
            {
                continue;
            }

            addressPeoKey*aKey=[[addressPeoKey alloc]init];
            aKey.count=nCount;
            aKey.phoneSet=[NSMutableSet setWithCapacity:0];

            people.phnum=[NSMutableArray arrayWithCapacity:0];

            for(int i = 0 ;i < nCount;i++)
            {
                NSString *phoneNO=[AddressEngine trimIlegalChar:[self trimIlegalChar:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(ref, i))]];
                if (phoneNO!=nil&&![phoneNO isEqualToString:@""])
                {
                    if ([phoneNO hasPrefix:@"+86"])
                    {
                        phoneNO=[phoneNO substringFromIndex:3];
                    }
                    [people.phnum addObject:phoneNO];

                    [aKey.phoneSet addObject:phoneNO];
                }
            }
            people.name=(__bridge NSString *)ABRecordCopyCompositeName(person);//名字
            if (people.name==nil)
            {
                people.name=@"姓名不详";
            }
            if (people.name.length>0)
            {
                NSString *firstChar = [people.name substringToIndex:1];
                const char * cString = [firstChar UTF8String];
                if (cString!=NULL)
                {
                    if (strlen(cString) == 3)
                    {
                        // 名字是汉字开头
                        NSMutableString *ms = [[NSMutableString alloc] initWithString:people.name];
                        CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformToLatin, NO);
                        //                    CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO);
                        people.pinyinName=[NSString stringWithString:ms];
                        people.pinyinName=[AddressEngine trimIlegalChar:people.pinyinName];
                        //                    NSLog(@"pinyin:%@",people.pinyinName);
                    }else
                    {
                        people.pinyinName=[people.name lowercaseString];
                    }
                }else
                {
                    people.name=[people.phnum objectAtIndex:0];
                    people.pinyinName=@"#FX";
                }
            }else
            {
                people.name=[people.phnum objectAtIndex:0];
                people.pinyinName=@"";
            }
            NSInteger rec=ABRecordGetRecordID(person);
            people.recordID=[NSString stringWithFormat:@"%ld",rec];

            people.remark=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonNoteProperty));//备注
            if (!people.remark)
            {
                people.remark=@"";
            }
            if (people.remark.length>0)
            {
                NSString*re=people.remark;
                people.remark=[AddressEngine cleanQuotesWithStr:re];
            }
            people.isChoose=NO;
            people.isMark=NO;

            [addrDic setObject:people forKey:aKey];
//            [addArray addObject:people];
        }
        //        NSLog(@"jieshu");
    }
    else
    {
        UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"权限受限"
                                                 message:@"设置-手机梵讯-启用通讯录权限"
                                                delegate:nil
                                       cancelButtonTitle:@"确定"
                                       otherButtonTitles:nil];
        [alert show];
    }
//    return addArray;
    return addrDic;
}

2.判断是否有权限

+(BOOL)getAccessGranted
{
    ABAddressBookRef addressBook = NULL;
    __block BOOL accessGranted = NO;

    if (&ABAddressBookRequestAccessWithCompletion != NULL)
    {
        addressBook = ABAddressBookCreateWithOptions(NULL, 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;
    }
    return accessGranted;
}
时间: 2024-11-29 04:20:32

ios 读取通讯录的相关文章

ios 读取通讯录数据

#import <Foundation/Foundation.h> @interface LoadingContactData : NSObject // 读取通讯录 + (LoadingContactData *)shareIntance; - (void)getContactData; @end #import "LoadingContactData.h" static LoadingContactData *_contactData; @implementation

iOS读取通讯录获取好友通讯录信息[名字(姓+名字),手机号码(多个号码)等]

一.创建通讯录对象 self.addressBook=ABAddressBookCreateWithOptions(NULL, NULL); //请求访问用户通讯录,注意无论成功与否block都会调用 ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error) { if (!granted) { NSLog(@"未获得通讯录访问权限!"); } }); ABPe

iOS通过app读取通讯录信息(整理)

iOS通过app读取通讯录信息,读取通讯录信息时需要加载AddressBookUI 和AddressBook两个包,并且引入头文件 #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> 具体实现如下: -(void)readAllPeoples { //定义通讯录名字为addressbook ABAddressBookRef tmpAddressBook = nil; //根据系统版本

iOS.访问通讯录.01.读取联系人信息

1.相关函数介绍 1.创建通讯录对象函数 ABAddressBookRef ABAddressBookCreateWithOptions( CFDictionaryRef options, CFErrorRef *error ); 例子: CFErrorRef error = NULL; ABAdressBookRef addressBook = ABAdressBookCreateWithOptions(NULL,&error); ABAddressBookRequestAccessWithC

iOS 修改通讯录联系人地址(address)崩溃原因分析

目前项目中需要对iOS系统通讯录进行读取,修改操作.在进行对地址修改的时候,出现了一个奇怪现象: ● 如果contact没有address字段(或者一个全新的contact),对它的address进行修改是可以成功的, ● 如果这个人有过address字段,此时对它就行修改就崩溃.控制台打出: *** -[CFString release]: message sent to deallocated instance 0x81d26f0 这应该是一个僵尸对象,重复释放某一个对象.首先我对修改通讯录

iOS中通讯录电话号码空格问题

今天在读取通讯录的时候,读取到的手机号码格式为* (***) ***-****的,乍看下,数字中间有空格."-".(.)的非数字字符. 然后我就打算替换这些非数字字符,结果替换完,发现空格还是在,单步调试下,替换也运行了,打印了下号码,发现其实那不是空格,是"·",这可能算是iOS的小坑吧. iOS中通讯录电话号码空格问题

ios 获取通讯录的所有信息

iOS获取通讯录全部信息 ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook); for(int i = 0; i < CFArrayGetCount(results); i++) { ABRecordRef person = CFArrayGetValueAtIndex(results, i); //读取f

IOS 读取本地的Json/plist 文件

一.一般本地可以存储轻量级数据存储 plist  这个主要是操作字典 方法如下: NSString * sampleFile= [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"sections_auth=4.plist"]; NSDictionary* dic_sections = [NSDictionary dictionaryWithContentsOfFile:sampleFile]; 二.

android中读取通讯录学习笔记

1.读取通讯录时一次读取时,尽量少读取所有属性,特别是列表展示的时候,会让你的列表加载速度变得难以忍受,建议先加载少量属性,然后在详情的时候加载所有属性. 2.在读取一类属性的时候,建议用一个游标,且放在循环外面,能明显加快速度,用projection(表示需要查询的列,在下面代码中是CONTACTOR_ION). 示例代码如下: private static final String[] CONTACTOR_ION = new String[]{ ContactsContract.Common