0

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.

Adi
  • 1
  • 1
  • Can you be a bit more specific on what means "it does not work"? Are there compile errors, runtime errors? What is the output? What have you already tried? – Axel Feb 04 '11 at 07:55
  • sorry this was unclear.... in the above code, 'bday' is printed as "(null)" by the console. On the other hand, 'today' is printed correctly (today's date & time). Did I call the ABRecordCopy Value incorrectly? – Adi Feb 04 '11 at 10:46
  • no errors, just that it returns NULL. – Adi Feb 04 '11 at 10:58
  • where do you get personRecord from? does it point to a valid location? – Axel Feb 04 '11 at 11:27
  • maybe the birthday property is not set, then it returns nil – Felix Feb 04 '11 at 11:29
  • Hi Axel and Phix23, I give the whole piece of code as below for more information about my code. Please have a look.... – Adi Feb 04 '11 at 12:11
  • Phix23, could you please tell the meaning of 'birthday property is not set' ? I'd like to check for any clue. thanks. – Adi Feb 04 '11 at 12:12
  • 1
    most likely the contact record that you are accessing does not have a birthday defined and so the NSDate* bday variable is nil; the code that you have is exactly what I use and it works fine –  Sep 23 '11 at 23:38

2 Answers2

0

This works for me:

NSDate* birthDate = (__bridge_transfer NSDate*)ABRecordCopyValue(addressBookContact, kABPersonBirthdayProperty);
HugoMasterPL
  • 209
  • 3
  • 10
0

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);
  }
Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38