标签:
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; }
标签:
原文地址:http://www.cnblogs.com/liaods/p/4932140.html