ios 读取通讯录数据

#import <Foundation/Foundation.h>

@interface LoadingContactData : NSObject

// 读取通讯录
+ (LoadingContactData *)shareIntance;

- (void)getContactData;

@end

#import "LoadingContactData.h"

static LoadingContactData *_contactData;

@implementation LoadingContactData

+ (LoadingContactData *)shareIntance
{
    if (_contactData == nil) {
        _contactData = [[LoadingContactData alloc]init];
    }
    return _contactData;
}

- (void)getContactData
{
    CFErrorRef error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    if (addressBook) {
        NSArray *allContacts = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
       NSMutableArray *contactsArray = [NSMutableArray new];

        for (NSUInteger i = 0; i<[allContacts count]; i++)
        {
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];

            // Get mobile number
            ABMultiValueRef phonesRef = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
            NSMutableArray *telephoneArray = [self getMobilePhoneProperty:phonesRef];

            [telephoneArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

                THContact *contact = [[THContact alloc] init];

                // Get first and last names and fullName
                NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
                NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
                NSString *fullName = (__bridge_transfer NSString *)ABRecordCopyCompositeName(contactPerson);

                // Set Contact properties
                contact.firstName = firstName;
                contact.lastName = lastName;

                if (fullName != nil){
                    contact.fullName = fullName;
                }
                else if(firstName != nil && lastName != nil) {
                    contact.fullName = [NSString stringWithFormat:@"%@%@",lastName,firstName];
                } else if (firstName != nil) {
                    contact.fullName = firstName;
                } else if (lastName != nil) {
                    contact.fullName = lastName;
                } else {
                    contact.fullName = @"";
                }

                // Get image if it exists
                NSData * imgData = (__bridge_transfer NSData *)ABPersonCopyImageDataWithFormat(contactPerson,kABPersonImageFormatThumbnail); // 联系人头像(裁剪后的)
                //NSData  *imgData = (__bridge_transfer NSData *)ABPersonCopyImageData(contactPerson);          // 联系人头像原图
                UIImage *userImg = [UIImage imageWithData:imgData];
                contact.image = userImg;

                if (!contact.image) {
                    contact.image = MF_PngWithSkin(DEFAULTS_SKIN, @"icon-avatar.png");
                }

                contact.phone = [obj telephoneWithReformat];

                if ([Unity validateMobile:contact.phone]) {
                    contact.recordId = ABRecordGetRecordID(contactPerson)+ [[contact.phone substringWithRange:NSMakeRange(2, 8)] integerValue]+idx;
                    [contactsArray addObject:contact];
                }

            }];

            if(phonesRef) {
                CFRelease(phonesRef);
            }

        }

        [[ContactDataArray shareContactData] setContactDataArray:contactsArray];

        if(addressBook) {
            CFRelease(addressBook);
        }
    }
    else
    {
        NSLog(@"Error");

    }
}

- (NSMutableArray *)getMobilePhoneProperty:(ABMultiValueRef)phonesRef
{
    NSMutableArray *dataArray = [[NSMutableArray alloc]init];
    for (int i=0; i < ABMultiValueGetCount(phonesRef); i++) {

        CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, i);
         NSString *phone = (__bridge NSString *)currentPhoneValue;
        [dataArray addObject:phone];
    }

    return dataArray;
}

@end
时间: 2024-10-06 00:02:24

ios 读取通讯录数据的相关文章

利用AddressBook.framework框架获取iOS系统通讯录数据

此方法是使用AddressBook.framework框架获取通讯录信息 第一步.在info.plist文件里面配置访问权限 第二步.导入头文件 1 #import <AddressBook/AddressBook.h> 2 #import <AddressBookUI/AddressBookUI.h> 第三步.获取通讯录数据 1 - (void)getSystemAddress{ 2 3 //新建一个通讯录类 4 ABAddressBookRef addressBooks = n

ios 读取通讯录

1.获取通讯录列表 +(NSMutableDictionary*)getAddressPeopleArray { BOOL granted = [AddressEngine getAccessGranted]; // NSMutableArray*addArray=[NSMutableArray arrayWithCapacity:0]; NSMutableDictionary * addrDic = [NSMutableDictionary dictionaryWithCapacity:0];

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

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

iOS读取图片数据的两种方法:UIImageJPEG和UIImagePNG

UIImageJPEGRepresentation函数有两个参数:引用的图片和压缩系数. UIImagePNGRepresentation(UIImage * image)函数只有一个引用图片参数. UIImagePNGRepresentation(UIImage * image)要比UIImageJPEGPresentation(UIImage * image, 1.0)返回的图片数据量大很多,如果对图片清晰度要求不高,可以通过设置UIImageJPEGReprentation的第二个参数,大

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模拟器将数据存储在本地沙盒中以及从沙盒中读取详细步骤

使用IO模拟器,应用沙盒的根路径为:/Users/apple/Library/Application Support/iPhone Simulator/6.0/Applications(6.0为模拟器的版本) 1.获取应用沙盒目录 <1>利用沙盒根目录拼接"Documents"字符串 NSString * home = NSHomeDirectory(); NSString * documents = [home stringByAppendingPathComponent

iOS彩票项目--第七天,初次读取json数据、KVC转模型技巧、运行时字典转模型以及初步对显示网页的操作并且跟踪标签

一.初次读取json数据 二.KVC转模型技巧,这里的技巧主要解决的是字典中的key 与 模型中有的属性对应不起来的时候 的解决办法 <方法1> <方法2>运行时字典转模型,运行时自己一直很晕.不过还是整理下来,方便以后用. 这里直接创建了一个分类. 头文件代码 1 // 2 // NSObject+Model.h 3 // Chaos_G 4 // 5 6 #import <Foundation/Foundation.h> 7 8 @interface NSObjec

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

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