如何以编程方式在 iOS 5 中保存生日日期
Posted
技术标签:
【中文标题】如何以编程方式在 iOS 5 中保存生日日期【英文标题】:How to save a birthday date in iOS 5 programmatically 【发布时间】:2013-10-10 13:37:25 【问题描述】:我在 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);
【问题讨论】:
你检查过你的日期不为空吗? 【参考方案1】:您可以为联系人设置生日(通过联系人应用程序),只有一个月/天..
检查一下,
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);
【讨论】:
以上是关于如何以编程方式在 iOS 5 中保存生日日期的主要内容,如果未能解决你的问题,请参考以下文章