2015-05-01 21 views
0

我正在構建一個iOS應用程序,我試圖獲取所有手機聯繫人並顯示它們。如何檢索從IOS中的通訊錄中的一個聯繫人保存的所有電話號碼?

現在的問題是,如果有兩個電話號碼與一個名稱保存的情況下,然後在我的列表中只顯示第一個號碼。

我需要檢索針對一個特定聯繫人保存的兩個號碼。

+0

請參閱本url:http://stackoverflow.com/questions/26015865/how-to-pick-a-contact-from-address-book-ios8 – Rahul

+0

農場動物關閉的另一個有用的問題 –

回答

0

上的地址簿爲iOS好的教程可以在這裏找到:

http://www.raywenderlich.com/63885/address-book-tutorial-in-ios

+0

請不要只發佈一個鏈接爲可以過時了。相反,將任何你需要說的東西添加到你的答案中。 – trojanfoe

+0

@trojanfoe好的,但請注意,原來的問題要求「教程或博客鏈接」 – Daniel

+0

@Daniel謝謝,但這個博客添加了我想要檢索聯繫人和顯示的聯繫人。 –

1

請檢查蘋果地址簿標準教程。它也將幫助你理解它的基礎知識。

https://developer.apple.com/library/ios/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/QuickStart.html

你可以得到所有類型的聯繫人中有[PJRAddressBook getPersonContacts]在我的sample.where你可以得到所有的電話號碼。像mobilePhone, MAINPHONE,iPhonePhone,homeFaxPhone,workFaxPhone,otherFaxPhone,pagerPhone

+0

請不要只發佈一個鏈接,因爲這可能會過時。相反,將任何你需要說的東西添加到你的答案中。 – trojanfoe

+0

@PJR謝謝,但這個博客添加了我想要檢索聯繫人和顯示的聯繫人。 –

+0

@sofquestion我分享我的一個組件,它會幫助你:https://github.com/paritsohraval100/PJRAddressBook – PJR

1

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; 
} 
+0

非常感謝你給我一個完整的例子,因爲我是新的IOS –

0
// 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; 
} 
+0

@Thanks Ankit - 我在[arrContact addObject:dict]中得到錯誤;是 - 使用未申報的未申報的'arrContat';你的意思是'allcontacts' –

+0

可以請你幫我解決這個問題 –

+0

定義arrContact first..like'NSMutableArray * myArr' –

相關問題