我正在構建一個iOS應用程序,我試圖獲取所有手機聯繫人並顯示它們。如何檢索從IOS中的通訊錄中的一個聯繫人保存的所有電話號碼?
現在的問題是,如果有兩個電話號碼與一個名稱保存的情況下,然後在我的列表中只顯示第一個號碼。
我需要檢索針對一個特定聯繫人保存的兩個號碼。
我正在構建一個iOS應用程序,我試圖獲取所有手機聯繫人並顯示它們。如何檢索從IOS中的通訊錄中的一個聯繫人保存的所有電話號碼?
現在的問題是,如果有兩個電話號碼與一個名稱保存的情況下,然後在我的列表中只顯示第一個號碼。
我需要檢索針對一個特定聯繫人保存的兩個號碼。
上的地址簿爲iOS好的教程可以在這裏找到:
http://www.raywenderlich.com/63885/address-book-tutorial-in-ios
請檢查蘋果地址簿標準教程。它也將幫助你理解它的基礎知識。
你可以得到所有類型的聯繫人中有[PJRAddressBook getPersonContacts]在我的sample.where你可以得到所有的電話號碼。像mobilePhone, MAINPHONE,iPhonePhone,homeFaxPhone,workFaxPhone,otherFaxPhone,pagerPhone
從shouldContinueAfterSelectingPerson
你可以得到的選擇person
參考方法,你就可以得到不同的電話號碼一樣,
multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(i=0; i< ABMultiValueGetCount(multi);i++)
{
NSMutableString *numb = (__bridge NSMutableString*)ABMultiValueCopyValueAtIndex(multi, i);
//next 5 line will make numbers are clear digit, without symbols.
numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@" " withString:@""]];
numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@"(" withString:@""]];
numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@")" withString:@""]];
numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@"-" withString:@""]];
numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@"." withString:@""]];
NSString *valAtIndex = (__bridge NSString *)ABMultiValueCopyLabelAtIndex(multi,i);
if([valAtIndex isEqualToString:@"_$!<Mobile>!$_"] || [valAtIndex isEqualToString:@"_$!<Other>!$_"])
mobileNumber = numb;
if ([valAtIndex isEqualToString:@"_$!<Home>!$_"])
homeNumber = numb;
if ([valAtIndex isEqualToString:@"_$!<Work>!$_"])
WorkNuimber = numb;
}
非常感謝你給我一個完整的例子,因爲我是新的IOS –
// Import two framework
#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>
// create a property
@property (nonatomic, assign) ABAddressBookRef addressBookRef;
// in view didLoad
ABAddressBookRequestAccessWithCompletion(self.addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[self getContactsFromAddressBook];
});
} else {
// TODO: Show alert
}
});
-(void)getContactsFromAddressBook
{
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook) {
NSArray *allContacts = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
for (i = 0; i<[allContacts count]; i++) {
// myClass *shrObj = [[myClass alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
// Get first and last names
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName= (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
if (!lastName) {
[email protected]"";
}
NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:fullName forKey:@"name"];
// Get mobile number
ABMultiValueRef phonesRef = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
if ([self getMobilePhoneProperty:phonesRef]!=nil) {
[dict setValue:[self getMobilePhoneProperty:phonesRef] forKey:@"mobile"];
[arrContact addObject:dict];
}
if(phonesRef) {
CFRelease(phonesRef);
}
}
[tblContact reloadData];
}
else
{
NSLog(@"Error");
}
}
- (NSString *)getMobilePhoneProperty:(ABMultiValueRef)phonesRef {
for (int i=0; i < ABMultiValueGetCount(phonesRef); i++) {
CFStringRef currentPhoneLabel = ABMultiValueCopyLabelAtIndex(phonesRef, 0);
CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, 0);
if(currentPhoneLabel) {
if (CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo) {
return (__bridge NSString *)currentPhoneValue;
}
if (CFStringCompare(currentPhoneLabel, kABHomeLabel, 0) == kCFCompareEqualTo) {
return (__bridge NSString *)currentPhoneValue;
}
}
if(currentPhoneLabel) {
CFRelease(currentPhoneLabel);
}
if(currentPhoneValue) {
CFRelease(currentPhoneValue);
}
}
return nil;
}
@Thanks Ankit - 我在[arrContact addObject:dict]中得到錯誤;是 - 使用未申報的未申報的'arrContat';你的意思是'allcontacts' –
可以請你幫我解決這個問題 –
定義arrContact first..like'NSMutableArray * myArr' –
請參閱本url:http://stackoverflow.com/questions/26015865/how-to-pick-a-contact-from-address-book-ios8 – Rahul
農場動物關閉的另一個有用的問題 –