2012-11-29 132 views
1

我得到的地址簿中,這樣所有的接觸點陣列:的iOS地址簿謂詞

NSMutableArray *records = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); 

什麼格式將謂詞是在說一個聯繫人的名字?我已經試過的記錄。在這個問題中建議:Search ABAddressbook iOS SDK但我得到一個未知的關鍵異常。數組中的項似乎爲__NSCFType類型。任何幫助將不勝感激。

回答

1

哎從設備獲取聯繫人信息,你也可以使用我的波紋管代碼聯繫豆類創建只是看到這個..

你也需要包括AddressBook.framework

#import <AddressBook/AddressBook.h> 
#import <AddressBook/ABAddressBook.h> 
#import <AddressBook/ABPerson.h> 

[contactList removeAllObjects]; 

// open the default address book. 
ABAddressBookRef m_addressbook = ABAddressBookCreate(); 
if (!m_addressbook) { 
    NSLog(@"opening address book"); 
} 

// can be cast to NSArray, toll-free 
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook); 
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook); 

// CFStrings can be cast to NSString! 

for (int i=0;i < nPeople;i++) { 
MContact *contact = [[MContact alloc] init]; 

ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i); 
CFStringRef firstName, lastName; 
firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty); 
lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty); 
contact.name = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 

ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty); 
if(ABMultiValueGetCount(eMail) > 0) { 
    contact.email = (NSString *)ABMultiValueCopyValueAtIndex(eMail, 0); 
    [contactList addObject:contact]; 
} 

CFRelease(ref); 
CFRelease(firstName); 
CFRelease(lastName); 


} 

和這裏MContact是波紋管

@interface MContact : NSObject { 
NSString *email; 
NSString *name; 
NSString *lastName; 
NSString *phone; 

BOOL isSelected; 
} 
@property (nonatomic, retain) NSString *email; 
@property (nonatomic, retain) NSString *name; 
@property (nonatomic, retain) NSString *lastName; 
@property (nonatomic, retain) NSString *phone; 

@property (nonatomic) BOOL isSelected; 
@property (nonatomic, readonly) NSString *displayName; 
@end 

我希望這會幫助你n對象(Bean)的文件......

0

馬里奧看看下面的代碼。我測試過了,它工作正常。

// Retrieving the address from address book... 
    ABAddressBookRef ref = ABAddressBookCreate(); 
    CFArrayRef getArr = ABAddressBookCopyArrayOfAllPeople(ref); 
    CFIndex totCount = CFArrayGetCount(getArr); 
    for (int m = 0; m < totCount; m++) 
    { 
     ABRecordRef recordRef = CFArrayGetValueAtIndex(getArr, m); 
     ABMultiValueRef names = ABRecordCopyValue(recordRef, kABPersonPhoneProperty); 
     NSString* getFirstName = (__bridge NSString *)names; 
     NSLog(@"The name is :-%@\n", getFirstName);  
     getFirstName = (__bridge NSString*)ABMultiValueCopyValueAtIndex(names, 1); 
     NSLog(@"The phone number is :-%@\n", getFirstName); 
    } 

如有任何問題,請回復我。

+0

嗨感謝,我的問題是如何將應用謂詞的getArr –

+0

不能我們NSPredicates與ABAddressBook –

+0

嘿馬里奧,如果你想使用NSPredicate,那麼你需要首先投CFArrayRef來NSMutableArray/NSArray,因爲CFArrayRef是免費的網橋,所以你可以實現它:一個CFArrayRef免費橋接到NSArray *,所以你可以把它轉換爲這樣,然後創建一個可變副本: NSMutableArray * convertedData =( __bridge NSArray *)getArr mutableCopy];然後,您可以像使用NSArray一樣使用NSPredicate。希望,這是你需要的。 :) –