【问题标题】:iOS address book predicateiOS 通讯录谓词
【发布时间】:2012-11-29 12:12:51
【问题描述】:

我得到了地址簿中所有联系人的数组:

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

谓词以什么格式表示联系人的名字?我已按照以下问题的建议尝试了 record.Search ABAddressbook iOS SDK,但我得到了一个未知的密钥异常。数组中的项目似乎是 __NSCFType 类型。任何帮助将不胜感激。

【问题讨论】:

    标签: iphone objective-c ios abaddressbook


    【解决方案1】:

    嘿,要从设备获取联系信息,您也可以将我的波纹管代码与我创建的 Contact bean 类一起使用,看看这个..

    您还需要包含 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 是下面的 NObject (Bean) 文件

    @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
    

    希望对你有帮助……

    【讨论】:

      【解决方案2】:

      马里奥看看下面的代码。我已经测试过了,它工作正常。

          // 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);
          }
      

      如有任何问题,请回复我。

      【讨论】:

      • 您好,谢谢,我的问题是您如何将谓词应用于 getArr
      • 你不能用 ABAddressBook 使用 NSPredicates
      • 嘿马里奥,如果你想使用 NSPredicate,那么你首先需要将 CFArrayRef 转换为 NSMutableArray/NSArray 并且由于 CFArrayRef 是免费桥接器,所以你可以实现它:A CFArrayRef is toll -free 桥接到 NSArray ,因此您可以将其转换为这样,然后创建一个可变副本: NSMutableArray *convertedData = (__bridge NSArray)getArr mutableCopy];然后,您可以像使用 NSArray 一样使用 NSPredicate。希望,这就是你需要的。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 2011-08-15
      相关资源
      最近更新 更多