ABAddressBook get birthday property

僤鯓⒐⒋嵵緔 提交于 2020-01-06 04:30:10

问题


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.


回答1:


This works for me:

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



回答2:


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


来源:https://stackoverflow.com/questions/4895094/abaddressbook-get-birthday-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!