1

我在 ios 7 的 iphone 联系人中保存了生日日期,但代码在 ios 5 中不起作用。还有其他方法吗?

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormat setDateFormat:@"dd MMM yyyy"];
NSDate * birdayDate =[dateFormat dateFromString:contactCardBirthdateLbl.text];

NSArray * nameArray = [contactCardNameLbl.text componentsSeparatedByString:@" "];
ABRecordSetValue(person, kABPersonFirstNameProperty,(__bridge CFTypeRef)([nameArray objectAtIndex:0]) , nil);
ABRecordSetValue(person, kABPersonLastNameProperty,(__bridge CFTypeRef)([nameArray objectAtIndex:1]), nil);
ABRecordSetValue(person, kABPersonJobTitleProperty,(__bridge CFTypeRef) contactCardOccupationLbl.text, nil);
ABRecordSetValue(person, kABPersonBirthdayProperty,(__bridge CFDateRef)birdayDate,nil);
4

1 回答 1

0

您可以为联系人设置生日(通过联系人应用程序),只有一个月/天。

检查这个,

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();

  NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [dateFormat setDateFormat:@"dd MMM yyyy"];
    NSDate * birdayDate =[dateFormat dateFromString:contactCardBirthdateLbl.text];

    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd.MM"];
    //don't set year in address book (yyyy=1604)


    NSDate *bdate=[formatter dateFromString:[formatter stringFromDate:birdayDate]];

NSArray * nameArray = [contactCardNameLbl.text componentsSeparatedByString:@" "];
ABRecordSetValue(person, kABPersonFirstNameProperty,(__bridge CFTypeRef)([nameArray objectAtIndex:0]) , nil);
ABRecordSetValue(person, kABPersonLastNameProperty,(__bridge CFTypeRef)([nameArray objectAtIndex:1]), nil);
ABRecordSetValue(person, kABPersonJobTitleProperty,(__bridge CFTypeRef) contactCardOccupationLbl.text, nil);
ABRecordSetValue(person, kABPersonBirthdayProperty,(__bridge CFDateRef)bdate,nil);
于 2013-10-10T13:44:14.047 回答