如何访问 IOS 联系人并在 Uitableview 中显示 [关闭]
Posted
技术标签:
【中文标题】如何访问 IOS 联系人并在 Uitableview 中显示 [关闭]【英文标题】:How to access IOS Contact and display in Uitableview [closed] 【发布时间】:2014-02-20 04:40:12 【问题描述】:我是 iphone 开发新手,我正在开发应用程序,它读取所有联系人并在 tableview 中显示,并读取所有日历事件并在 tableview 中显示。
【问题讨论】:
ABAddressBook.framework 的使用教程和小伙伴们有很多。请将您的问题缩小到一个具体问题。 要挑选你的联系人,使用苹果开发指南:developer.apple.com/library/ios/documentation/ContactData/… zcentric.com/2008/09/19/access-the-address-book 试试吧.. 【参考方案1】:添加地址簿框架。
#import AddressBook/AddressBook.h然后我有一个工作示例:您必须根据您的要求更改代码:
-(void)loadContact
dispatch_async(dispatch_get_main_queue(), ^(void)
if (loadingView == nil)
loadingView = [LoadingView loadingViewInSuperView:self.view];
);
ABAddressBookRef ab;
ab = ABAddressBookCreate();
int len = (int) ABAddressBookGetPersonCount(ab);
NSMutableArray *allPeople = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(ab);
int i;
for(i = 0; i < len ; i++)
// Initialize the dictionary
NSMutableDictionary *dictAddress = [[NSMutableDictionary alloc] init];
ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);
[dictAddress setValue:@"0" forKey:@"sel"];
[dictAddress setValue:@"" forKey:@"FirstName"];
[dictAddress setValue:@"" forKey:@"LastName"];
[dictAddress setValue:@"" forKey:@"Name"];
[dictAddress setValue:@"" forKey:@"PhoneMobile"];
[dictAddress setValue:@"" forKey:@"PhoneHome"];
[dictAddress setValue:@"" forKey:@"PhoneWork"];
[dictAddress setValue:@"" forKey:@"CompanyName"];
[dictAddress setValue:@"" forKey:@"AddressCity"];
[dictAddress setValue:@"" forKey:@"AddressCountry"];
[dictAddress setValue:@"" forKey:@"AddressState"];
[dictAddress setValue:@"" forKey:@"AddressCountryCode"];
[dictAddress setValue:@"" forKey:@"AddressStreet"];
[dictAddress setValue:@"" forKey:@"AddressZipCode"];
[dictAddress setValue:@"" forKey:@"EmailWork"];
[dictAddress setValue:@"" forKey:@"EmailHome"];
[dictAddress setValue:@"" forKey:@"EmailOther"];
[dictAddress setValue:@"" forKey:@"URLWork"];
[dictAddress setValue:@"" forKey:@"URLHome"];
[dictAddress setValue:@"" forKey:@"URLOther"];
[dictAddress setValue:@"" forKey:@"Note"];
NSMutableString *xmlString = [NSMutableString string];
if(ABRecordCopyValue(person, kABPersonFirstNameProperty) != NULL)
[dictAddress setValue:(NSString*)CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty)) forKey:@"FirstName"];
[xmlString appendString:(__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty)];
if(ABRecordCopyValue(person, kABPersonLastNameProperty) != NULL)
[dictAddress setValue:(__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty) forKey:@"LastName"];
[xmlString appendString:(__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty)];
[dictAddress setValue:xmlString forKey:@"Name"];
if(ABRecordCopyValue(person, kABPersonOrganizationProperty) != NULL)
CFStringRef company = ABRecordCopyValue(person, kABPersonOrganizationProperty);
if(company)
[dictAddress setValue:(__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty) forKey:@"CompanyName"];
CFRelease(company);
if (ABRecordCopyValue(person, kABPersonPhoneProperty) != NULL)
ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty));
NSString* Str=@"";
NSString* phoneLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
phoneLabel = (__bridge_transfer NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
//if([phoneLabel isEqualToString:@"_$!<Main>!$_"])
if([phoneLabel isEqualToString:@"_$!<Home>!$_"])
Str = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phones, i);
Str = [Str stringByReplacingOccurrencesOfString:@"(" withString:@""];
Str = [Str stringByReplacingOccurrencesOfString:@")" withString:@""];
Str = [Str stringByReplacingOccurrencesOfString:@"-" withString:@""];
Str =[[Str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
[dictAddress setValue:Str forKey:@"PhoneHome"];
else if([phoneLabel isEqualToString:@"_$!<Mobile>!$_"])
Str = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phones, i);
Str = [Str stringByReplacingOccurrencesOfString:@"(" withString:@""];
Str = [Str stringByReplacingOccurrencesOfString:@")" withString:@""];
Str =[[Str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
Str = [Str stringByReplacingOccurrencesOfString:@"-" withString:@""];
[dictAddress setValue:Str forKey:@"PhoneMobile"];
//else if([phoneLabel isEqualToString:@"_$!<Other>!$_"])
else if([phoneLabel isEqualToString:@"_$!<Work>!$_"])
Str = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phones, i);
Str = [Str stringByReplacingOccurrencesOfString:@"(" withString:@""];
Str = [Str stringByReplacingOccurrencesOfString:@")" withString:@""];
Str =[[Str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
Str = [Str stringByReplacingOccurrencesOfString:@"-" withString:@""];
[dictAddress setValue:Str forKey:@"PhoneWork"];
CFRelease(phones);
if (ABRecordCopyValue(person, kABPersonAddressProperty) != NULL)
//Person address
ABMultiValueRef addresses = ABRecordCopyValue(person, kABPersonAddressProperty);
if(ABMultiValueGetCount(addresses) != 0)
for(int i=0;i<ABMultiValueGetCount(addresses);i++)
CFStringRef address = ABMultiValueCopyValueAtIndex(addresses, i);
NSString *StrCity = [NSString stringWithFormat:@"%@", [(__bridge NSDictionary*)address objectForKey:@"City"]];
[dictAddress setValue:StrCity forKey:@"AddressCity"];
NSString *StrCountry = [NSString stringWithFormat:@"%@", [ (__bridge
NSDictionary*)address objectForKey:@"Country"]];
[dictAddress setValue:StrCountry forKey:@"AddressCountry"];
NSString *StrState = [NSString stringWithFormat:@"%@",[(__bridge NSDictionary*)address objectForKey:@"State"]];
[dictAddress setValue:StrState forKey:@"AddressState"];
NSString *StrCountryCode = [NSString stringWithFormat:@"%@",[(__bridge NSDictionary*)address objectForKey:@"CountryCode"]];
[dictAddress setValue:StrCountryCode forKey:@"AddressCountryCode"];
NSString *StrStreet = (NSString*)[(__bridge NSDictionary*)address objectForKey:@"Street"];
[dictAddress setValue:StrStreet forKey:@"AddressStreet"];
NSString *StrZip = [NSString stringWithFormat:@"%@",[(__bridge NSDictionary*)address objectForKey:@"ZIP"]];
[dictAddress setValue:StrZip forKey:@"AddressZipCode"];
else
CFRelease(addresses);
if (ABRecordCopyValue(person, kABPersonEmailProperty) != NULL)
//person email
NSString* emailLabel;
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
if(ABMultiValueGetCount(emails) != 0)
for(int i=0;i<ABMultiValueGetCount(emails);i++)
emailLabel = (__bridge_transfer NSString*)ABMultiValueCopyLabelAtIndex(emails, i);
if ([emailLabel isEqualToString:@"_$!<Work>!$_"])
[dictAddress setValue:(__bridge id)(ABMultiValueCopyValueAtIndex(emails, i)) forKey:@"EmailWork"];
else if([emailLabel isEqualToString:@"_$!<Home>!$_"])
[dictAddress setValue:(__bridge id)(ABMultiValueCopyValueAtIndex(emails, i)) forKey:@"EmailHome"];
else if([emailLabel isEqualToString:@"_$!<Other>!$_"])
[dictAddress setValue:(__bridge id)(ABMultiValueCopyValueAtIndex(emails, i)) forKey:@"EmailOther"];
else
CFRelease(emails);
if(ABRecordCopyValue(person, kABPersonURLProperty) != NULL)
NSString* URLLabel;
ABMultiValueRef urls = ABRecordCopyValue(person, kABPersonURLProperty);
if(ABMultiValueGetCount(urls) != 0)
for(int i=0;i<ABMultiValueGetCount(urls);i++)
URLLabel = (__bridge_transfer NSString*)ABMultiValueCopyLabelAtIndex(urls, i);
if ([URLLabel isEqualToString:@"_$!<Work>!$_"])
[dictAddress setValue:(__bridge id)(ABMultiValueCopyValueAtIndex(urls, i)) forKey:@"URLWork"];
else if([URLLabel isEqualToString:@"_$!<Home>!$_"])
[dictAddress setValue:(__bridge id)(ABMultiValueCopyValueAtIndex(urls, i)) forKey:@"URLHome"];
else if([URLLabel isEqualToString:@"_$!<Other>!$_"])
[dictAddress setValue:(__bridge id)(ABMultiValueCopyValueAtIndex(urls, i)) forKey:@"URLOther"];
if(ABRecordCopyValue(person, kABPersonNoteProperty) != NULL)
[dictAddress setValue:(__bridge NSString*)ABRecordCopyValue(person, kABPersonNoteProperty) forKey:@"Note"];
if (ABRecordCopyValue(person, kABPersonCreationDateProperty) != NULL)
[dictAddress setValue:(__bridge NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty) forKey:@"CreationDate"];
if (ABRecordCopyValue(person, kABPersonModificationDateProperty) != NULL)
[dictAddress setValue:(__bridge NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty) forKey:@"ModificationDate"];
/*
if (![[dictAddress valueForKey:@"EmailHome"] isEqualToString:@""] || ![[dictAddress valueForKey:@"EmailOther"] isEqualToString:@""] || ![[dictAddress valueForKey:@"EmailWork"] isEqualToString:@""])
dictAddress = nil;
*/
NSLog(@"%@",dictAddress);
NSMutableArray *numberDict = [[NSMutableArray alloc]init];
if (![[dictAddress valueForKey:@"PhoneHome"] isEqualToString:@""])
// [numberDict setValue:[dictAddress valueForKey:@"PhoneHome"] forKey:@"PhoneHome"];
[numberDict addObject:[dictAddress valueForKey:@"PhoneHome"]];
if (![[dictAddress valueForKey:@"PhoneMobile"] isEqualToString:@""])
// [numberDict setValue:[dictAddress valueForKey:@"PhoneMobile"] forKey:@"PhoneMobile"];
[numberDict addObject:[dictAddress valueForKey:@"PhoneMobile"]];
if (![[dictAddress valueForKey:@"PhoneWork"] isEqualToString:@""])
[numberDict addObject:[dictAddress valueForKey:@"PhoneWork"]];
// [numberDict setValue:[dictAddress valueForKey:@"PhoneWork"] forKey:@"PhoneWork"];
[dictPhoneNumberFinal setObject:numberDict forKey:[dictAddress valueForKey:@"FirstName"]];
dispatch_async(dispatch_get_main_queue(), ^(void)
if (loadingView)
[loadingView removeView];
loadingView = nil;
);
//arrContactNames = [[dictPhoneNumberFinal allKeys] mutableCopy];
arrContactNames =[[[dictPhoneNumberFinal allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] mutableCopy];
if(arrContactNames.count != 0)
[tblView reloadData];
// UIView *view = [[UIView alloc] initWithFrame:self.tableView.frame];
// [view setUserInteractionEnabled:FALSE];
//
// [view setBackgroundColor:[UIColor clearColor]];
// [self.tableView addSubview:view];
// [self.tableView setScrollEnabled:NO];
// [self addPopOverViewWithTag:1 :arrContactNames];
else
[appDelegate userAlert:@"No contacts available."];
【讨论】:
我可以得到你的工作示例吗..我正在使用故事板所以遇到问题【参考方案2】:使用 ABAddressBook 框架。以数组的形式获取联系人列表并将此列表填充到 UItableview 中,您可以使用 UITableViewCellAccessoryCheckmark 显示选定的 ABAddressBook 联系人。
【讨论】:
非常感谢您的快速回复....您有上述实现的任何链接 ***.com/questions/19027118/fetch-contacts-in-ios-7看看这个答案 如果有任何问题请告诉我 你有任何工作示例 edumobile.org/iphone/iphone-beginner-tutorials/… 使用这个会帮助你,这个gabriel-tips.blogspot.in/2012/02/…以上是关于如何访问 IOS 联系人并在 Uitableview 中显示 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章