您的位置:首页 > 移动开发 > IOS开发

IOS疯狂基础之 通讯录

2015-05-15 16:37 302 查看
ABAddressBookRef addressBook =ABAddressBookCreate();

__block
BOOL accessGranted =NO;

if (ABAddressBookRequestAccessWithCompletion !=NULL)
{

// we're on iOS 6

NSLog(@"on iOS 6 or later, trying to grant access permission");

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);

dispatch_release(sema);
}

else {// we're on iOS 5 or older

NSLog(@"on iOS 5 or older, it is OK");
accessGranted =YES;
}
if (accessGranted) {

NSLog(@"we got the access right");
}else{
return;
}

//ABAddressBookRef addressBook = ABAddressBookCreate();

CFArrayRef friendList =ABAddressBookCopyArrayOfAllPeople(addressBook);

UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollationcurrentCollation];//这个是建立索引的核心

allFriends = [NSMutableArrayarrayWithCapacity:1];
int friendsCount =
CFArrayGetCount(friendList);
NSLog(@"%d",friendsCount);

NSMutableArray *temp = [NSMutableArrayarrayWithCapacity:0];
for (int i =0; i<friendsCount; i++) {

NameIndex *item = [[NameIndexalloc]
init];//NameIndex是一个用于给UILocalizedIndexedCollation类对象做索引的类,代码见下个代码块
ABRecordRef record =
CFArrayGetValueAtIndex(friendList, i);

CFStringRef firstName =ABRecordCopyValue(record,kABPersonFirstNameProperty);

CFStringRef lastName = ABRecordCopyValue(record,kABPersonLastNameProperty);

//获取号码多值
ABRecordRef person =
CFArrayGetValueAtIndex(friendList, i);

ABMultiValueRef phone =ABRecordCopyValue(person,kABPersonPhoneProperty);
item._lastName = (__bridgeNSString*)lastName;
item._firstName = (__bridgeNSString*)ABMultiValueCopyValueAtIndex(phone,0);//获取第一个号码
item._originIndex = i;
(lastName==NULL)?:CFRelease(lastName);
(firstName==NULL)?:CFRelease(firstName);
[tempaddObject:item];
}

for (NameIndex *itemin temp) {
NSInteger sect = [theCollation
sectionForObject:item
collationStringSelector:@selector(getLastName)];//getLastName是实现中文安拼音检索的核心,见NameIndex类
item._sectionNum = sect;//设定姓的索引编号
}
NSInteger highSection = [[theCollation
sectionTitles] count];
//返回的应该是27,是a-z和#
NSMutableArray *sectionArrays = [NSMutableArrayarrayWithCapacity:highSection];
//tableView 会被分成27个section
for (int i=0; i<=highSection; i++) {
NSMutableArray *sectionArray = [NSMutableArrayarrayWithCapacity:1];
[sectionArraysaddObject:sectionArray];
}
for (NameIndex *itemin temp) {
[(NSMutableArray *)[sectionArraysobjectAtIndex:item._sectionNum]addObject:item];
}
for (NSMutableArray *sectionArrayin sectionArrays) {
NSArray *sortedSection = [theCollation
sortedArrayFromArray:sectionArray
collationStringSelector:@selector(getFirstName)];//同
[allFriendsaddObject:sortedSection];
}

[self.mainTablereloadData];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

return [[UILocalizedIndexedCollationcurrentCollation]
sectionIndexTitles];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if ([[allFriendsobjectAtIndex:section]
count] > 0) {

return [[[UILocalizedIndexedCollationcurrentCollation]
sectionTitles]objectAtIndex:section];
}

return
nil;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString
*)title atIndex:(NSInteger)index
{

return [[UILocalizedIndexedCollationcurrentCollation]
sectionForSectionIndexTitleAtIndex:index];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return [allFriendscount];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [(NSArray*)[allFriendsobjectAtIndex:section]
count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {

static
NSString *CellIdentifier =@"Cell";

UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==
nil) {

cell = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier] ;
}

// Set up the cell...

cell.textLabel.text = [NSStringstringWithFormat:@"%@%@",((NameIndex*)[[allFriendsobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row])._lastName
,((NameIndex*)[[allFriendsobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row])._firstName];

return cell;
}

@end

//获得通讯录中联系人的所有属性

//

//ABAddressBookRef addressBook = ABAddressBookCreate();

//

//CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);

//

//for(int i = 0; i < CFArrayGetCount(results); i++)

//{

// ABRecordRef person = CFArrayGetValueAtIndex(results, i);

// //读取firstname

// NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

// if(personName != nil)

// textView.text = [textView.text stringByAppendingFormat:@"\n姓名:%@\n",personName];

// //读取lastname

// NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

// if(lastname != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastname];

// //读取middlename

// NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);

// if(middlename != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlename];

// //读取prefix前缀

// NSString *prefix = (NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty);

// if(prefix != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",prefix];

// //读取suffix后缀

// NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty);

// if(suffix != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",suffix];

// //读取nickname呢称

// NSString *nickname = (NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty);

// if(nickname != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",nickname];

// //读取firstname拼音音标

// NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);

// if(firstnamePhonetic != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",firstnamePhonetic];

// //读取lastname拼音音标

// NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);

// if(lastnamePhonetic != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastnamePhonetic];

// //读取middlename拼音音标

// NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty);

// if(middlenamePhonetic != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlenamePhonetic];

// //读取organization公司

// NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);

// if(organization != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",organization];

// //读取jobtitle工作

// NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty);

// if(jobtitle != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",jobtitle];

// //读取department部门

// NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty);

// if(department != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",department];

// //读取birthday生日

// NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);

// if(birthday != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",birthday];

// //读取note备忘录

// NSString *note = (NSString*)ABRecordCopyValue(person, kABPersonNoteProperty);

// if(note != nil)

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",note];

// //第一次添加该条记录的时间

// NSString *firstknow = (NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty);

// NSLog(@"第一次添加该条记录的时间%@\n",firstknow);

// //最后一次修改該条记录的时间

// NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);

// NSLog(@"最后一次修改該条记录的时间%@\n",lastknow);

//

// //获取email多值

// ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);

// int emailcount = ABMultiValueGetCount(email);

// for (int x = 0; x < emailcount; x++)

// {

// //获取email Label

// NSString* emailLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x));

// //获取email值

// NSString* emailContent = (NSString*)ABMultiValueCopyValueAtIndex(email, x);

// textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",emailLabel,emailContent];

// }

// //读取地址多值

// ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);

// int count = ABMultiValueGetCount(address);

//

// for(int j = 0; j < count; j++)

// {

// //获取地址Label

// NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtIndex(address, j);

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",addressLabel];

// //获取該label下的地址6属性

// NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);

// NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];

// if(country != nil)

// textView.text = [textView.text stringByAppendingFormat:@"国家:%@\n",country];

// NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];

// if(city != nil)

// textView.text = [textView.text stringByAppendingFormat:@"城市:%@\n",city];

// NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];

// if(state != nil)

// textView.text = [textView.text stringByAppendingFormat:@"省:%@\n",state];

// NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];

// if(street != nil)

// textView.text = [textView.text stringByAppendingFormat:@"街道:%@\n",street];

// NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];

// if(zip != nil)

// textView.text = [textView.text stringByAppendingFormat:@"邮编:%@\n",zip];

// NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];

// if(coutntrycode != nil)

// textView.text = [textView.text stringByAppendingFormat:@"国家编号:%@\n",coutntrycode];

// }

//

// //获取dates多值

// ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty);

// int datescount = ABMultiValueGetCount(dates);

// for (int y = 0; y < datescount; y++)

// {

// //获取dates Label

// NSString* datesLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y));

// //获取dates值

// NSString* datesContent = (NSString*)ABMultiValueCopyValueAtIndex(dates, y);

// textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",datesLabel,datesContent];

// }

// //获取kind值

// CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);

// if (recordType == kABPersonKindOrganization) {

// // it's a company

// NSLog(@"it's a company\n");

// } else {

// // it's a person, resource, or room

// NSLog(@"it's a person, resource, or room\n");

// }

//

//

// //获取IM多值

// ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);

// for (int l = 1; l < ABMultiValueGetCount(instantMessage); l++)

// {

// //获取IM Label

// NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l);

// textView.text = [textView.text stringByAppendingFormat:@"%@\n",instantMessageLabel];

// //获取該label下的2属性

// NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l);

// NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];

// if(username != nil)

// textView.text = [textView.text stringByAppendingFormat:@"username:%@\n",username];

//

// NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];

// if(service != nil)

// textView.text = [textView.text stringByAppendingFormat:@"service:%@\n",service];

// }

//

// //读取电话多值

// ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);

// for (int k = 0; k<ABMultiValueGetCount(phone); k++)

// {

// //获取电话Label

// NSString * personPhoneLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));

// //获取該Label下的电话值

// NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, k);

//

// textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",personPhoneLabel,personPhone];

// }

//

// //获取URL多值

// ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);

// for (int m = 0; m < ABMultiValueGetCount(url); m++)

// {

// //获取电话Label

// NSString * urlLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));

// //获取該Label下的电话值

// NSString * urlContent = (NSString*)ABMultiValueCopyValueAtIndex(url,m);

//

// textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",urlLabel,urlContent];

// }

//

// //读取照片

// NSData *image = (NSData*)ABPersonCopyImageData(person);

//

//

// UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(200, 0, 50, 50)];

// [myImage setImage:[UIImage imageWithData:image]];

// myImage.opaque = YES;

// [textView addSubview:myImage];

//

//

//

//}

//

//CFRelease(results);

//CFRelease(addressBook);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: