我是新開發的iphone,我正在研究讀取所有聯繫人並在tableview中顯示並讀取所有日曆事件並在tableview中顯示的應用程序。如何訪問iOS聯繫人並在Uitableview中顯示
-2
A
回答
1
使用ABAddressBook框架。以數組形式獲取聯繫人列表並在UItableview中填充此列表,並且可以使用UITableViewCellAccessoryCheckmark來顯示選定的聯繫人ABAddressBook。
2
添加通訊錄框架工作。
- #進口通訊錄/ 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* [email protected]"";
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."];
}
}
+0
我可以得到你的工作示例..我使用故事板,所以得到問題 – user1374
相關問題
- 1. 在UItableview中顯示聯繫人
- 2. 如何顯示聯繫人在IOS
- 3. 訪問ios中的聯繫人
- 4. 流行的請求訪問聯繫人不顯示ios 9
- 5. 如何在插入聯繫人後禁用聯繫人顯示
- 6. 請求允許訪問聯繫人iOS
- 7. 訪問聯繫人iOS目標c
- 8. 訪問最近的聯繫人iOS 9
- 9. 如何查詢IOS聯繫人聯繫人使用新聯繫人框架顯示訂單設置
- 10. 在ios中使用系統框架顯示iPhone聯繫人
- 11. 如何使用Ionic聯繫人顯示聯繫人圖片
- 12. 如何顯示聯繫人ID的聯繫人照片?
- 13. 如何訪問電話簿聯繫人?
- 14. 無法在設備訪問聯繫人源在iOS 6中
- 15. iOS通過UnitTest訪問AddressBook聯繫人;如何設置權限?
- 16. 訪問聯繫人數據
- 17. 訪問FAcebook聯繫人
- 18. 訪問WP7聯繫人
- 19. 如何突出顯示Outlook聯繫人
- 20. 添加的聯繫人不在聯繫人顯示在Android的
- 21. 獲取關係並在UITableView中顯示
- 22. 如何顯示iOS從Xamarin Forms添加聯繫人屏幕?
- 23. UITableView與聯繫人圖像問題
- 24. 在Jquerymobile中顯示聯繫人Listview(phonegap)
- 25. 在android listview中顯示聯繫人
- 26. 在imageview中顯示聯繫人圖片。
- 27. TableViewController不顯示聯繫人
- 28. PhoneGap-聯繫人未顯示
- 29. UITableView郵件聯繫人
- 30. 如何在Windows Phone應用程序8.0中訪問聯繫人?
有很多關於如何使用ABAddressBook.framework和朋友的教程。請將您的問題縮小到一個特定問題。 – warrenm
要挑選您使用的聯繫人Apple App開發指南:https://developer.apple.com/library/ios/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/QuickStart.html#//apple_ref/doc/uid/TP40007744-CH2- SW1 –
http://zcentric.com/2008/09/19/access-the-address-book/試試吧.. – ChenSmile