Ios 如何从通讯录中的联系人中选择生日标签?
我正在使用通讯录获取带有标签的联系人列表。(如手机、主机、家庭、工作、传真等)。我从联系人那里拿到了电话、电子邮件标签,但我没有拿到生日、周年纪念的标签。这是我的生日班代码Ios 如何从通讯录中的联系人中选择生日标签?,ios,objective-c,iphone,Ios,Objective C,Iphone,我正在使用通讯录获取带有标签的联系人列表。(如手机、主机、家庭、工作、传真等)。我从联系人那里拿到了电话、电子邮件标签,但我没有拿到生日、周年纪念的标签。这是我的生日班代码 ABMultiValueRef dateofbirth1 = ABRecordCopyValue(contactPerson, kABPersonBirthdayProperty); // Assign the Date Of birth NSString *dob1=[NSDateF
ABMultiValueRef dateofbirth1 = ABRecordCopyValue(contactPerson, kABPersonBirthdayProperty); // Assign the Date Of birth
NSString *dob1=[NSDateFormatter localizedStringFromDate:(__bridge NSDate *)(dateofbirth1) dateStyle:NSDateFormatterLongStyle timeStyle:0]; // Changing to string format using Date Formatter.
if(!(dob1==nil))
{
// DOB is Not Nill
}
这是我的生日标签代码
if([arrayOfDatesAsStrings count]>0)
{
for (int j = 0; j < [arrayOfDatesAsStrings count] ; j++)
{
**//This is the fetching birthday label code and following code is crashed**
CFStringRef locLabel1 = ABMultiValueCopyLabelAtIndex((__bridge ABMultiValueRef)(dob1), j);
NSString *phoneLabel1 =(__bridge NSString*) ABAddressBookCopyLocalizedLabel(locLabel1);
personD.dateOfBirth = phone1;
NSLog(@" %@ %@",phoneLabel1,personD.dateOfBirth);
}
}
else
{
NSLog(@"Date Of Birth was Not set ");
}
if([ArrayOfDateSasString计数]>0)
{
对于(int j=0;j<[ArrayOfDateSasString计数];j++)
{
**//这是提取生日标签代码,以下代码已崩溃**
CFStringRef Localabel1=Ab多值CopyLabelaIndex((uu桥ABMultiValueRef)(dob1),j);
NSString*phoneLabel1=(uu桥NSString*)ABAddressBookCopyLocalizedLabel(LocalLabel1);
personD.dateOfBirth=phone1;
NSLog(@“%@%@”,电话标签1,个人出生日期);
}
}
其他的
{
NSLog(“未设定出生日期”);
}
我能为此做些什么?有人能帮我吗?崩溃错误消息为:
“线程1:Exc\u BAD\u访问(代码=1,地址=0x38)”从设备获取联系人
if (isIOS9) { //checking iOS version of Device
CNContactStore *store = [[CNContactStore alloc] init];
//keys with fetching properties
NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactEmailAddressesKey,CNContactPostalAddressesKey, CNLabelWork, CNLabelDateAnniversary];
NSString *containerId = store.defaultContainerIdentifier;
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
NSError *error;
NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
DLOG(@"cnContacts %lu",(unsigned long)cnContacts.count);
if (error) {
//error
} else {
for (CNContact *contact in cnContacts) {
//iterate over cnContacts to get details
}
}
} else {
//for below iOS 9
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef arrPersons = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex count = ABAddressBookGetPersonCount(addressBook);
NSLog(@"cnContacts %lu",(unsigned long)count);
for (int i = 0; i < count; i++) {
ABRecordRef record = CFArrayGetValueAtIndex(arrPersons,i);
//use kABPersonBirthdayProperty to get b’day
NSString *birthDay = (__bridge NSString *)(ABRecordCopyValue(record, kABPersonBirthdayProperty));
NSLog(@“B’day %@”, birthDay);
}
}
if(isIOS9){//检查设备的iOS版本
CNContactStore*store=[[CNContactStore alloc]init];
//具有抓取属性的键
NSArray*keys=@[CNContactFamilyNameKey、CNContactGivenNameKey、CNContactPhoneNumbersKey、CNContactEmailAddressKey、CNContactPostLaddresseSKey、CNLabelWork、CNLabelDate周年纪念];
NSString*containerId=store.defaultContainerIdentifier;
NSPredicate*谓词=[CNContact谓词ForContactsInContainerWithiIdentifier:containerId];
n错误*错误;
NSArray*cnContacts=[store unifiedContactsMatchingPredicate:predicate keystefetch:keys error:&error];
DLOG(@“cnContacts%lu”,(无符号长)cnContacts.count);
如果(错误){
//错误
}否则{
用于(CNContact*cnContacts中的联系人){
//迭代cnContacts以获取详细信息
}
}
}否则{
//对于低于iOS 9的用户
ABAddressBookRef addressBook=ABAddressBookCreate();
CFArrayRef arrPersons=abAddressBookCopyArrayFallPerson(地址簿);
CFIndex count=ABAddressBookGetPersonCount(地址簿);
NSLog(@“cnContacts%lu”,(无符号长)计数);
for(int i=0;i
如果崩溃,错误消息是什么?线程1:Exc\u BAD\u访问(代码=1,地址=0x38)