下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
IOS关于通讯录的开发有两种,一种是直接调用系统的通讯录界面,根据回调信息处理数据,另一种是直接获取系统的通讯录,完全自定义UI,并且可以通过官方给我们提供的接口进行读写。这篇博客主要讨论第二种方式。 IOS获取系统通讯录联系人信息一、权限注册
//这个变量用于记录授权是否成功,即用户是否允许我们访问通讯录 int __block tip=0; //声明一个通讯簿的引用 ABAddressBookRef addBook =nil; //因为在IOS6.0之后和之前的权限申请方式有所差别,这里做个判断 if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) { //创建通讯簿的引用 addBook=ABAddressBookCreateWithOptions(NulL, NulL); //创建一个出事信号量为0的信号 dispatch_semaphore_t sema=dispatch_semaphore_create(0); //申请访问权限 ABAddressBookRequestAccessWithCompletion(addBook, ^(bool greanted, CFErrorRef error) { //greanted为YES是表示用户允许,否则为不允许 if (!greanted) { tip=1; } //发送一次信号 dispatch_semaphore_signal(sema); }); //等待信号触发 dispatch_semaphore_wait(sema, disPATCH_TIME_FOREVER); }else{ //IOS6之前 addBook =ABAddressBookCreate(); } if (tip) { //做一个友好的提示 UIAlertVIEw * alart = [[UIAlertVIEw alloc]initWithTitle:@"温馨提示" message:@"请您设置允许APP访问您的通讯录\nSettings>General>Privacy" delegate:self cancelbuttonTitle:@"确定" otherbuttonTitles:nil, nil]; [alart show]; return; }二、获取通讯录联系人详细信息
//获取所有联系人的数组 CFArrayRef alllinkPeople = ABAddressBookcopyArrayOfAllPeople(addBook); //获取联系人总数 CFIndex number = ABAddressBookGetPersonCount(addBook); //进行遍历 for (NSInteger i=0; i<number; i++) { //获取联系人对象的引用 ABRecordRef people = CFArrayGetValueAtIndex(alllinkPeople, i); //获取当前联系人名字 Nsstring*first@R_403_6889@=(__brIDge Nsstring *)(ABRecordcopyValue(people, kABPersonFirst@R_403_6889@Property)); //获取当前联系人姓氏 Nsstring*last@R_403_6889@=(__brIDge Nsstring *)(ABRecordcopyValue(people, kABPersonLast@R_403_6889@Property)); //获取当前联系人中间名 Nsstring*mIDdle@R_403_6889@=(__brIDge Nsstring*)(ABRecordcopyValue(people, kABPersonMIDdle@R_403_6889@Property)); //获取当前联系人的名字前缀 Nsstring*prefix=(__brIDge Nsstring*)(ABRecordcopyValue(people, kABPersonPrefixProperty)); //获取当前联系人的名字后缀 Nsstring*suffix=(__brIDge Nsstring*)(ABRecordcopyValue(people, kABPersonSuffixProperty)); //获取当前联系人的昵称 Nsstring*nick@R_403_6889@=(__brIDge Nsstring*)(ABRecordcopyValue(people, kABPersonNick@R_403_6889@Property)); //获取当前联系人的名字拼音 Nsstring*first@R_403_6889@Phoneic=(__brIDge Nsstring*)(ABRecordcopyValue(people, kABPersonFirst@R_403_6889@PhoneticProperty)); //获取当前联系人的姓氏拼音 Nsstring*last@R_403_6889@Phoneic=(__brIDge Nsstring*)(ABRecordcopyValue(people, kABPersonLast@R_403_6889@PhoneticProperty)); //获取当前联系人的中间名拼音 Nsstring*mIDdle@R_403_6889@Phoneic=(__brIDge Nsstring*)(ABRecordcopyValue(people, kABPersonMIDdle@R_403_6889@PhoneticProperty)); //获取当前联系人的公司 Nsstring*organization=(__brIDge Nsstring*)(ABRecordcopyValue(people, kABPersonorganizationProperty)); //获取当前联系人的职位 Nsstring*job=(__brIDge Nsstring*)(ABRecordcopyValue(people, kABPersonJobTitleProperty)); //获取当前联系人的部门 Nsstring*department=(__brIDge Nsstring*)(ABRecordcopyValue(people, kABPersonDepartmentProperty)); //获取当前联系人的生日 Nsstring*birthday=(__brIDge NSDate*)(ABRecordcopyValue(people, kABPersonBirthdayProperty)); NSMutableArray * emailArr = [[NSMutableArray alloc]init]; //获取当前联系人的邮箱 注意是数组 ABMultiValueRef emails= ABRecordcopyValue(people, kABPersonEmailProperty); for (NSInteger j=0; j<ABMultiValueGetCount(emails); j++) { [emailArr addobject:(__brIDge Nsstring *)(ABMultiValuecopyValueAtIndex(emails, j))]; } //获取当前联系人的备注 Nsstring*notes=(__brIDge Nsstring*)(ABRecordcopyValue(people, kABPersonNoteProperty)); //获取当前联系人的电话 数组 NSMutableArray * phoneArr = [[NSMutableArray alloc]init]; ABMultiValueRef phones= ABRecordcopyValue(people, kABPersonPhoneProperty); for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) { [phonerr addobject:(__brIDge Nsstring *)(ABMultiValuecopyValueAtIndex(phones, j))]; } //获取创建当前联系人的时间 注意是NSDate NSDate*creatTime=(__brIDge NSDate*)(ABRecordcopyValue(people, kABPersonCreationDateProperty)); //获取最近修改当前联系人的时间 NSDate*alterTime=(__brIDge NSDate*)(ABRecordcopyValue(people, kABPersonModificationDateProperty)); //获取地址 ABMultiValueRef address = ABRecordcopyValue(people, kABPersonAddressproperty); for (int j=0; j<ABMultiValueGetCount(address); j++) { //地址类型 Nsstring * type = (__brIDge Nsstring *)(ABMultiValuecopyLabelAtIndex(address, j)); NSDictionary * temDic = (__brIDge NSDictionary *)(ABMultiValuecopyValueAtIndex(address, j)); //地址字符串,可以按需求格式化 Nsstring * adress = [Nsstring stringWithFormat:@"国家:%@\n省:%@\n市:%@\n街道:%@\n邮编:%@",[temDic valueForKey:(Nsstring*)kABPersonAddressCountryKey],[temDic valueForKey:(Nsstring*)kABPersonAddressstateKey],[temDic valueForKey:(Nsstring*)kABPersonAddressCityKey],[temDic valueForKey:(Nsstring*)kABPersonAddressstreetKey],[temDic valueForKey:(Nsstring*)kABPersonAddressZIPKey]]; } //获取当前联系人头像图片 NSData*userImage=(__brIDge NSData*)(ABPersoncopyImageData(people)); //获取当前联系人纪念日 NSMutableArray * dateArr = [[NSMutableArray alloc]init]; ABMultiValueRef dates= ABRecordcopyValue(people, kABPersonDateProperty); for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) { //获取纪念日日期 NSDate * data =(__brIDge NSDate*)(ABMultiValuecopyValueAtIndex(dates, j)); //获取纪念日名称 Nsstring * str =(__brIDge Nsstring*)(ABMultiValuecopyLabelAtIndex(dates, j)); NSDictionary * temDic = [NSDictionary dictionaryWithObject:data forKey:str]; [dateArr addobject:temDic]; }
//相关人,组织字段const ABPropertyID kABPersonKindProperty; const CFNumberRef kABPersonKindPerson;const CFNumberRef kABPersonKindOrganization;// 电话相关字段AB_EXTERN const ABPropertyID kABPersonPhoneProperty;AB_EXTERN const CFStringRef kABPersonPhoneMobileLabel;AB_EXTERN const CFStringRef kABPersonPhoneIPhoneLabel AB_EXTERN const CFStringRef kABPersonPhoneMainLabel;AB_EXTERN const CFStringRef kABPersonPhoneHomeFAXLabel;AB_EXTERN const CFStringRef kABPersonPhoneWorkFAXLabel;AB_EXTERN const CFStringRef kABPersonPhoneOtherFAXLabelAB_EXTERN const CFStringRef kABPersonPhonePagerLabel;// 即时聊天信息相关字段AB_EXTERN const ABPropertyID kABPersonInstantMessageProperty; AB_EXTERN const CFStringRef kABPersonInstantMessageServiceKey; AB_EXTERN const CFStringRef kABPersonInstantMessageServiceYahoo;AB_EXTERN const CFStringRef kABPersonInstantMessageServiceJabber;AB_EXTERN const CFStringRef kABPersonInstantMessageServiceMSN;AB_EXTERN const CFStringRef kABPersonInstantMessageServiceICQ;AB_EXTERN const CFStringRef kABPersonInstantMessageServiceaim;AB_EXTERN const CFStringRef kABPersonInstantMessageServiceQQ AB_EXTERN const CFStringRef kABPersonInstantMessageServiceGoogleTalk;AB_EXTERN const CFStringRef kABPersonInstantMessageServiceSkype;AB_EXTERN const CFStringRef kABPersonInstantMessageServiceFacebook;AB_EXTERN const CFStringRef kABPersonInstantMessageServiceGaduGadu;AB_EXTERN const CFStringRef kABPersonInstantMessageUser@R_403_6889@Key; // 个人网页相关字段AB_EXTERN const ABPropertyID kABPersonURLProperty;AB_EXTERN const CFStringRef kABPersonHomePageLabel; //相关人姓名字段AB_EXTERN const ABPropertyID kABPersonRelated@R_403_6889@sProperty; AB_EXTERN const CFStringRef kABPersonFatherLabel; // FatherAB_EXTERN const CFStringRef kABPersonMotherLabel; // MotherAB_EXTERN const CFStringRef kABPersonParentLabel; // ParentAB_EXTERN const CFStringRef kABPersonbrotherLabel; // brotherAB_EXTERN const CFStringRef kABPersonSisterLabel; // SisterAB_EXTERN const CFStringRef kABPersonChildLabel; // ChildAB_EXTERN const CFStringRef kABPersonFrIEndLabel; // FrIEndAB_EXTERN const CFStringRef kABPersonSpouseLabel; // SpouseAB_EXTERN const CFStringRef kABPersonPartnerLabel; // PartnerAB_EXTERN const CFStringRef kABPersonAssistantLabel; // AssistantAB_EXTERN const CFStringRef kABPersonManagerLabel; // Manager三、通讯录“写”的相关 *** 作
1、创建一个联系人
//创建一个联系人引用 ABRecordRef person = ABPersonCreate(); Nsstring *first@R_403_6889@ = @"哈"; Nsstring *last@R_403_6889@ = @"哈"; // 电话号码数组 NSArray *phones = [NSArray arrayWithObjects:@"123",@"456",nil]; // 电话号码对应的名称 NSArray *labels = [NSArray arrayWithObjects:@"iphone",@"home",nil]; //这里的字段和上面的字段完全相同 // 设置名字属性 ABRecordSetValue(person, kABPersonFirst@R_403_6889@Property,(__brIDge CFStringRef)first@R_403_6889@, NulL); // 设置姓氏属性 ABRecordSetValue(person, kABPersonLast@R_403_6889@Property, (__brIDge CFStringRef)last@R_403_6889@, NulL); // 设置生日属性 ABRecordSetValue(person, kABPersonBirthdayProperty,(__brIDge CFDateRef)birthday, NulL); // 字典引用 ABMultiValueRef dic =ABMultiValueCreateMutable(kABMultiStringPropertyType); // 添加电话号码与其对应的名称内容 for (int i = 0; i < [phones count]; i ++) { ABMultiValueIDentifIEr obj = ABMultiValueAddValueAndLabel(dic,(__brIDge CFStringRef)[phones objectAtIndex:i], (__brIDge CFStringRef)[labels objectAtIndex:i], &obj); } // 设置phone属性 ABRecordSetValue(person, kABPersonPhoneProperty, dic, NulL); // 将新建的联系人添加到通讯录中 ABAddressBookAddRecord(addBook, person, NulL); // 保存通讯录数据 ABAddressBookSave(addBook, NulL);2、修改联系人
3.删除联系人
//获取所有联系人 NSArray *array = (__brIDge NSArray*)ABAddressBookcopyArrayOfAllPeople(addBook); // 遍历所有的联系人 for (ID obj in array) { ABRecordRef people = (__brIDge ABRecordRef)obj; Nsstring *first@R_403_6889@ = (__brIDge Nsstring*)ABRecordcopyValue(people, kABPersonFirst@R_403_6889@Property); Nsstring *last@R_403_6889@ = (__brIDge Nsstring*)ABRecordcopyValue(people, kABPersonLast@R_403_6889@Property); if ([first@R_403_6889@ isEqualToString:@"哈"] &&[last@R_403_6889@ isEqualToString:@"哈"]) { ABAddressBookRemoveRecord(addBook, people,NulL); } } // 保存修改的通讯录对象 ABAddressBookSave(addBook, NulL);四、重中之重-关于内存管理
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的iOS获取通讯录联系人信息全部内容,希望文章能够帮你解决iOS获取通讯录联系人信息所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)