Adi
Adi

Reputation: 1

ABAddressBook get birthday property

I'm trying to get the birthday property from ABAddressBook on my iphone. I've looked through some discussions over the web and mostly recommends the same answer, which I have tried myself as below. But this still doesn't work for me so I wonder if i missed something else....

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

any help is much appreciated.. Thanks.

Upvotes: 0

Views: 1789

Answers (2)

Pradhyuman sinh
Pradhyuman sinh

Reputation: 3928

Try this:

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

Upvotes: 0

HugoMasterPL
HugoMasterPL

Reputation: 219

This works for me:

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

Upvotes: 0

Related Questions