2016-03-15 29 views
-1

我使用地址簿獲取聯繫人列表及其標籤。 (如手機,主,家庭,工作,傳真等)。我從聯繫人中獲取電話,電子郵件標籤,但我沒有獲取生日,週年紀念標籤。這是我的生日課程代碼。如何從地址簿中的聯繫人中選擇生日標籤?

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

我可以爲此做些什麼?誰能幫我?崩潰錯誤消息: 「主題1:EXC_BAD_ACCESS(代碼= 1,地址= 0x38)」

+0

如果崩潰,什麼是錯誤消息的聯繫人? – Larme

+0

線程1:Exc_BAD_ACCESS(代碼= 1,地址= 0x38) – sarosar

回答

0

要提取設備

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

} 
相關問題