ABAddressBook获取生日财产

时间:2011-02-04 06:11:44

标签: iphone xcode addressbook

我正试图从我的iphone上的ABAddressBook获取生日财产。我通过网络查看了一些讨论,并且大多数建议使用相同的答案,我在下面尝试过。但是这对我来说仍然不起作用,所以我想知道我是否错过了其他的东西......

        dayFormatter = [[[NSDateFormatter alloc] init]autorelease];
    [self.dayFormatter setDateFormat:@"MMMM d"];        

    NSString* today = [dayFormatter stringFromDate:[NSDate date]];
    NSLog(@"today:%@", today);
    NSLog(@"date:%@", [NSDate date]); // this works fine


    NSDate *bday = (NSDate*)ABRecordCopyValue(personRecord,kABPersonBirthdayProperty);
    NSLog(@"bdayyyy:%@", bday); // this doesn't work.


    NSString* personBday = [dayFormatter stringFromDate:bday];
    NSLog(@"Bday:%@", personBday);

非常感谢任何帮助..谢谢。

2 个答案:

答案 0 :(得分:0)

这对我有用:

NSDate* birthDate = (__bridge_transfer NSDate*)ABRecordCopyValue(addressBookContact, kABPersonBirthdayProperty);

答案 1 :(得分:0)

试试这个:

 ABAddressBookRef myAddressBook = ABAddressBookCreate();
 NSArray *allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(myAddressBook);

 for (id record in allPeople) {
     NSMutableDictionary *newRecord = [[NSMutableDictionary alloc] init];
     CFTypeRef bDayProperty = ABRecordCopyValue((ABRecordRef)record, kABPersonBirthdayProperty);

     if (ABRecordCopyValue((ABRecordRef)record, kABPersonBirthdayProperty)) 
       {
         NSDate *date=(NSDate*)bDayProperty;
         [newRecord setObject:date forKey:@"birthDate"];
         date=nil;
         [date release]; 
     }
    CFRelease(myAddressBook);
  }