0

Im using the code below to try and make a new contact in the addressBook, for some reason when i get to the ABAddressBookAddRecord, the didset returns false and i can not figure out why. Are there functions i forgot to call because i feel like this should work?

ABRecordRef record = ABPersonCreate();
CFErrorRef err = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(nil, &err);

bool didset;
didset = ABRecordSetValue(record, kABPersonFirstNameProperty, CFSTR("test"), &err);
didset = ABRecordSetValue(record, kABPersonLastNameProperty, CFSTR("monkey"), &err);
didset = ABRecordSetValue(record, kABPersonPhoneProperty, CFSTR("123-123-1234"), &err);

didset = ABAddressBookAddRecord(addressBook, record, &err);

if (ABAddressBookHasUnsavedChanges(addressBook))
{
    didset = ABAddressBookSave(addressBook, &err);
}
IanTimmis
  • 815
  • 1
  • 8
  • 16

1 Answers1

0

First you should implement UserAccess to your AddressBook.

There are my post about it:

Fetch Contacts in iOS 7

Community
  • 1
  • 1
Anton
  • 3,102
  • 2
  • 28
  • 47
  • Do you have any information that could lead me to that? ive been looking through apples address book programming guide and cant find anything that can lead me in the right direction to something like that – IanTimmis Jul 19 '14 at 05:32
  • see my updated answer, put it in ViewDidLoad in your controller – Anton Jul 19 '14 at 06:04
  • contactData it's separated class I am doing some operation with address book. You could replace it with your var emptyDictionary: CFDictionaryRef? var addressBook: ABAddressBookRef? – Anton Jul 19 '14 at 06:16
  • I have access to the contacts but i'm still getting EXC_BAD_ACCESS(code = 2, blah blah) for some reason – IanTimmis Jul 19 '14 at 06:38
  • You should read what the reason. From my experience. It's still not working well. Because beta. at least with adressbook framework – Anton Jul 19 '14 at 06:43
  • Originally i was asking the question in Objective-C and i tried the same thing and it wasnt working. i just tried swift because that was the answer you had provided for me – IanTimmis Jul 19 '14 at 06:44
  • In Objective-C I have working class that fetching all contact data I needed. Yes there it's working. But Swift, LLVM are still in beta there are a lot of bugs. – Anton Jul 19 '14 at 06:49
  • Would you like Accesscode for Objective-C? Sorry I thought you are trying with Swift :) Updated my answer with link to other post – Anton Jul 19 '14 at 06:49
  • Thank you for all the help. But will this allow me to programmatically create a new contact in the addressbook?? – IanTimmis Jul 19 '14 at 07:53
  • It's only fetching. But there is a code to access your address book – Anton Jul 19 '14 at 08:37
  • I tried it and i still get the EXC_BAD_ACESS(code=2, address = 0x10) error and it says that i DO have access through that code?? – IanTimmis Jul 19 '14 at 18:38
  • I thing you are something doing wrong in your code. You'd read real reason in your error code. https://developer.apple.com/library/mac/documentation/userexperience/conceptual/AddressBook/Tasks/ManagingGroups.html - try this Apple documentation – Anton Jul 19 '14 at 22:07