ios读取通讯录信息

(2012-05-22 14:07:11)

标签:

ios读取通讯录

it

 
iphone如许app读取通讯录信息,读取通讯录信息时需要加载AddressBookUI 和AddressBook两个包,具体实现方法如下

ABAddressBookRef addressBook = ABAddressBookCreate();//定义通讯录名字为addressbook
    CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);//将通讯录中的信息用数组方式读出
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);//获取通讯录中联系人
    
    iphoneContactList = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i = 0; i < nPeople; i++)
    {
        
        IphoneContact * iphoneContact = [[IphoneContact alloc] init];
        
        NSData *imageData = [[[NSData alloc] init]autorelease];
        NSString *address = [[[NSString alloc] init]autorelease];
        
        
        ABRecordRef person = CFArrayGetValueAtIndex(contacts, i);//取出某一个人的信息
       NSInteger lookupforkey =(NSInteger)ABRecordGetRecordID(person);//读取通讯录中联系人的唯一标识
       NSDate * createDate = (NSDate *)ABRecordCopyValue(person, kABPersonCreationDateProperty);// 读取通讯录中联系人的创建日期
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSString* birthDay = [formatter stringFromDate:createDate];
        [formatter release];
      
        
        
        NSString *createDate1 = [birthDay stringByReplacingOccurrencesOfString:@"-" withString:@""];
        NSString *createDate2 = [createDate1 stringByReplacingOccurrencesOfString:@":" withString:@""];
        NSString *createDate3 = [createDate2 stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSLog(@"1111========%@",createDate3);
        NSLog(@"222222========%@",birthDay);
        NSString *indexInIphone = [NSString stringWithFormat:@"%i",lookupforkey];
        iphoneContact.lookUpKey = [createDate3 stringByAppendingString:indexInIphone];
       //上诉操作是将某个联系人的标识号与创建日期进行组合,得到唯一的标识,是为了当时特殊的需要,一般不会有这种变态应用,这是因为ABRecordGetRecordID在一个手机中是唯一的,即使删掉某一个联系人,这个号也不会在被占用。
        
        
        
              //读取联系人姓名属性
        if (ABRecordCopyValue(person, kABPersonLastNameProperty)&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))== nil) {
            iphoneContact.contactName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
        }else if (ABRecordCopyValue(person, kABPersonLastNameProperty) == nil&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))){
            iphoneContact.contactName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        }else if (ABRecordCopyValue(person, kABPersonLastNameProperty)&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))){
            
            NSString *first =(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
            NSString *last = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
            iphoneContact.contactName = [NSString stringWithFormat:@"%@%@",last,first];
        }                     
         //读取联系人姓名的第一个汉语拼音,用于排序,调用此方法需要在程序中加载pingyin.h pingyin.c,如有需要请给我联系。     
        iphoneContact.pinyin =[[NSString stringWithFormat:@"%c",pinyinFirstLetter([iphoneContact.contactName characterAtIndex:0])] uppercaseString];
        
        
        //读取联系人公司信息                    
        
        iphoneContact.contactCompany = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
        
        //读取联系人工作
        iphoneContact.contactJob = (NSString *)ABRecordCopyValue(person, kABPersonJobTitleProperty);
        
        
        
        
        //读取联系人email信息,email有好几种,比如工作邮箱,家庭邮箱,通过ABMultiValueCopyLabelAtIndex 属性来判断,下面给出了例子                
        ABMultiValueRef emailForWORK = ABRecordCopyValue(person, kABPersonEmailProperty);
        if ((emailForWORK != nil)&&ABMultiValueGetCount(emailForWORK)>0) {
            
            for (int k = 0; k < ABMultiValueGetCount(emailForWORK); k++) {
                NSString * aEmail = (NSString *)ABMultiValueCopyValueAtIndex(emailForWORK, k);
                NSString * aEmailLabel = (NSString *)ABMultiValueCopyLabelAtIndex(emailForWORK, k);
                
                if ([aEmailLabel isEqualToString:@"_$!<Work>!$_"]) {
                    iphoneContact.contactEmail = aEmail;
                }       
            }
        }
        
        
        //读取通信地址信息,在ios中国家,省份,城市,区,街道,号都是单独存储的,需要单独读取然后在组合(根据需要)
        ABMultiValueRef addressTotal = ABRecordCopyValue(person, kABPersonAddressProperty);
        if (addressTotal) {
            
            int count = ABMultiValueGetCount(addressTotal);    
            
            for(int j = 0; j < count; j++)
            {
                
                NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(addressTotal, j);        
                NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
                if(country != nil)
                    address = [NSString stringWithFormat:@"%@",country];
                NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
                if(city != nil)
                    address = [address stringByAppendingFormat:@"%@",city];
                NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
                if(state != nil)
                    address = [address stringByAppendingFormat:@"%@",state];
                NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
                if(street != nil)
                    address = [address stringByAppendingFormat:@"%@",street];
                 
            }  
            
            iphoneContact.contactAdress = address;
        }
        
        
        
        
        //读取电话信息,和emial类似,也分为工作电话,家庭电话,工作传真,家庭传真。。。。
        
        ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
        
        if ((phone != nil)&&ABMultiValueGetCount(phone)>0) {
            
            for (int m = 0; m < ABMultiValueGetCount(phone); m++) {
                NSString * aPhone = [(NSString *)ABMultiValueCopyValueAtIndex(phone, m) autorelease];
                NSString * aLabel = [(NSString *)ABMultiValueCopyLabelAtIndex(phone, m) autorelease];
                
                if ([aLabel isEqualToString:@"_$!<Mobile>!$_"]) {
                    iphoneContact.contactMobile= aPhone;
                    
                }
                
                if ([aLabel isEqualToString:@"_$!<WorkFAX>!$_"]) {
                    iphoneContact.contactFax = aPhone;
                }
                
                if ([aLabel isEqualToString:@"_$!<Work>!$_"]) {
                    iphoneContact.contactTelephone= aPhone;
                }       
            }
        }
        
        
        
        
        //读取照片信息
        imageData = (NSData *)ABPersonCopyImageData(person);
        UIImage *myImage = [UIImage imageWithData:imageData];
        CGSize newSize = CGSizeMake(55, 55);
        UIGraphicsBeginImageContext(newSize);
        [myImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();//上诉代码实现图片压缩,可根据需要选择,现在是压缩到55*55
        
        imageData = UIImagePNGRepresentation(newImage);
        
        if (imageData) {
            NSData * BimageData = [GTMBase64 encodeData:imageData];
            iphoneContact.headImage= [[NSString alloc] initWithData:BimageData encoding:NSUTF8StringEncoding];
        }
               
        iphoneContact.isSelected = NO;
        [iphoneContactList addObject:iphoneContact];  //将读取的某一个人信息放到我们自定义的可变数组中        
        
    }

ios读取通讯录信息的更多相关文章

  1. ios 读取通讯录数据

    #import <Foundation/Foundation.h> @interface LoadingContactData : NSObject // 读取通讯录 + (Loading ...

  2. android 读取通讯录显示到gridview

    ........... <GridView android:id="@+id/gridView1" android:layout_width="match_pare ...

  3. iOS访问通讯录开发-读取联系人信息

    读取通信录中的联系人一般的过程是先查找联系人记录,然后再访问记录的属性,属性又可以分为单值属性和多值属性.通过下面例子介绍联系人的查询,以及单值属性和多值属性的访问,还有读取联系人中的图片数据. 本案 ...

  4. React Native之获取通讯录信息并实现类通讯录列表(ios android)

    React Native之获取通讯录信息并实现类通讯录列表(ios android) 一,需求分析 1,获取通讯录信息,筛选出通讯录里有多少好友在使用某个应用. 2,获取通讯录信息,实现类通讯录,可拨 ...

  5. iOS获取通讯录所有联系人信息

    以下是2种方式: 第一种方法: GetAddressBook.h #import <Foundation/Foundation.h> @interface GetAddressBook : ...

  6. IOS 获取通讯录中信息

    获取通讯录中信息 一. 我们设置一个ABAddressBookRef类型的属性addressBook. 二. 要获得通讯录中的信息,我们需要获取访问通讯录的权限. 在运行下面的获取权限的方法的时候,系 ...

  7. ios 获取通讯录的所有信息

    iOS获取通讯录全部信息 ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBoo ...

  8. iOS 获得通讯录中联系人的所有属性--b

    ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfA ...

  9. iOS有关通讯录操作

    一.首先获取用户通讯录授权信息. 在AppDelegate中导入#import <AddressBook/AddressBook.h>框架,在下列方法中获取授权信息. - (BOOL)ap ...

随机推荐

  1. js方法和原型继承(一)

    在js语言规范中并不存在方法这一概念,方便起见,将作为对象属性的函数成为方法this引用的规则a.在最外层代码中,this引用的是全局对象b.在函数内,this引用根据函数调用方式不同而不同函数内部的 ...

  2. thinkphp 字段静态验证$_validate中错误提醒多语言化写成{%LANGUATE}的原因

    class UserModel extends Model{ protected $_validate =  array( array('account', 'require', '{%LANGUAG ...

  3. HTML5 学习笔记 1

    1.音频.视频 <!DOCTYPE HTML> <html> <body> <audio controls="controls"> ...

  4. C# 平时碰见的问题【1】

    1. SqlBulkCopy 可以利用这个类实现快速大批量新增数据的效果, 但在使用过程中发现了一个问题: 无法将数据源中的DateTime类型转换成数据库中的int类型 看起来就是数据列不对应导致的 ...

  5. 如何让dapper支持oracle游标呢?

    Dapper是一个轻型的ORM类.它有啥优点.缺点相信很多朋友都知道了,园里也有很多朋友都有相关介绍,这里就不多废话. 如果玩过Oracle都知道,存储过程基本都是通过游标返回数据的,但是dapper ...

  6. 问题记录-Fragment导包不同导致无法自动转型

    代码如下 public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle save ...

  7. STM32F0xx_看门狗(独立+窗口)配置详细过程

    Ⅰ.概述 对于看门狗,我觉得做单片机或者嵌入式开发的人员来说并不陌生,今天总结STM32F0看门狗的功能,F0的看门狗有两种:独立和窗口看门狗. 今天提供两种看门狗的软件工程实例,供大家下载. 两种看 ...

  8. JavaScript高级程序设计之函数

    函数实际上是对象,每个函数都是Function类型的实例. 函数是引用类型. 函数名实际上是一个指向函数对象的指针,不会与某个函数绑定. // 这种写法更能表达函数的本质 var sum = func ...

  9. ios中怎么样调节占位文字与字体大小在同一高度

    在设置好字体以后,在占位文字中设置leading这个字体属性,用leading来乘以一个比例(CGFloat)来调节位置.

  10. [笔记]学习HighCharts的使用(不错的web图表插件)

    最近有一个小项目需要用到折线图.到处请教了一下,有人给我推荐了highcharts.感觉还不错,就稍微学习下.这里记录一下学习的过程. 网上相关的内容还不少,我就说一下我学习的内容. 看的第一篇文章& ...