I'm trying to create a new contact and add it to the AddressBook but when I get to the ABAddressSave line of code I get EXC_BAD_ACCESS. I cannot see what am I doing wrong, I enabled NSZombie to check if this is a memory related error but it didn't spot any. Can anybody tell me what is wrong with this code? Thank you in advance!
CFErrorRef error = NULL;
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newRecord = ABPersonCreate();
ABRecordSetValue(newRecord, kABPersonFirstNameProperty, #"Xxxxxx", &error);
ABRecordSetValue(newRecord, kABPersonURLProperty, #"Yyyyyy", &error);
//Add phone numbers to record
ABMutableMultiValueRef phones = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phones, #"1-555-555-5555", kABWorkLabel, NULL);
ABRecordSetValue(newRecord, kABPersonPhoneProperty, phones, &error);
CFRelease(phones);
//Add email address to record
ABMutableMultiValueRef emails = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emails, #"xxx_xxx#yahoo.com", kABWorkLabel, NULL);
ABRecordSetValue(newRecord, kABPersonEmailProperty, emails, &error);
CFRelease(emails);
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
[addressDict setObject:#"xxx1" forKey:(NSString *)kABPersonAddressStreetKey];
[addressDict setObject:#"xxx2" forKey:(NSString *)kABPersonAddressCityKey];
[addressDict setObject:#"xxx3" forKey:(NSString *)kABPersonAddressStateKey];
[addressDict setObject:#"xxx4" forKey:(NSString *)kABPersonAddressZIPKey];
ABMultiValueAddValueAndLabel(multiAddress, addressDict, kABWorkLabel, NULL);
ABRecordSetValue(newRecord, kABPersonAddressProperty, multiAddress, &error);
CFRelease(multiAddress);
[addressDict release];
ABAddressBookAddRecord(iPhoneAddressBook, newRecord, &error);
ABAddressBookSave(iPhoneAddressBook, NULL);
if(error != nil){
NSLog(#"Error creating contact:%#", error);
}
I would suggest running your code in Instruments with the Memory->Object Allocations template. It should very quickly show you which object is the problem and what memory deallocation is causing the problem.
Ok I've figured it out, it wasn't a memory issue, actually the error is not even in the posted code because when I posted the code I cleaned it a little bit and the error it is not there anymore. Kinda stupid but I did It. So the error: when I was assigning an URL value I was assigning it with a simple ABRecordSetValue call and I should've used an ABMutableMultiValueRef. Also, everytime I was filling the record with a nil value I got a crash, so I think nil values aren't allowed when you build your Person object. Thanks you for your time!
Related
Is there any framework/library available in iOS SDK to read/parse data in vCard format?
I am receiving data in a vcard format in a NSString and I have to parse it.
Googled a lot, but couldn't find solution yet.
BEGIN:VCARD
VERSION:2.1
N:LastName;FirstName;MiddleName;Prefix;Sufix
ADR;TYPE=HOME: postbox;street2;street1;city;state;zip;country
BDAY:2010-08-19
END:VCARD
I did found some solutions for you...
Have a look at the following links...
1) Want ready made sample code==>Click here
2) Writing to Vcard==>Click here
Code Relevent to you:
ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book record
ABRecordRef person = ABPersonCreate(); // create a person
NSString *phone = #"0123456789"; // the phone number to add
//Phone number is a list of phone number, so create a multivalue
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,phone,kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(person, kABPersonFirstNameProperty, #"FirstName" , nil); // first name of the new person
ABRecordSetValue(person, kABPersonLastNameProperty, #"LastName", nil); // his last name
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, &anError); // set the phone number property
ABAddressBookAddRecord(addressBook, person, nil); //add the new person to the record
ABRecordRef group = ABGroupCreate(); //create a group
ABRecordSetValue(group, kABGroupNameProperty,#"My Group", &error); // set group's name
ABGroupAddMember(group, person, &error); // add the person to the group
ABAddressBookAddRecord(addressBook, group, &error); // add the group
ABAddressBookSave(addressBook, nil); //save the record
CFRelease(person); // relase the ABRecordRef variable
3) Importing vCard sample code==>Click here
4) For creating custom parser ==> Click here
OS X v10.11 and iOS 9 introduces CNContactVCardSerialization which makes parsing a VCard a breeze.
I am successfully adding contact address in iPhone through my application by using below code
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef Showroom = ABPersonCreate();
ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
[addressDictionary setObject:[NSString stringWithFormat:#"%#",Address_Bcard.text] forKey:(NSString *) kABPersonAddressStreetKey];
ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL);
ABRecordSetValue(Showroom, kABPersonAddressProperty, multiAddress,#"");
CFRelease(multiAddress);
ABAddressBookAddRecord(addressBook, Showroom, nil);
ABAddressBookSave(addressBook, nil)
CFRelease(Showroom);
CFRelease(addressBook);
I am adding australian address from application to iPhone contacts eg. Sydney Australia. Instead of Sydney Australia the iPhone contact shows Sydney Australia India in addess field. How to remove this bugg.
If you don't specify the country in the address field of the contact it will automatically takes the country you live.
A way to specify the country code to set another country is:-
Change to
Settings -> General -> International -> Region Format to "Australia".
But the problem is this will change all the other contacts also.
There is also a discussion on apple site. https://discussions.apple.com/thread/2659179?start=0&tstart=0
I have an app that uses the user's Address Book for contacts. I also have other information I wish to store on a per-user basis (per Address Book entry basis). I allow the user to import a single user, an Address Book group, or all of their contacts. Because I wish to continue to allow outside applications to change the users Address Book, I do not import all the information. Instead I have chosen to alter the Address Book entry for each imported user, adding a kABPersonInstantMessageProperty key. I want populate to this key with MyAppsRecordLocaterNumber#MyAppsDomain.com as the username. I figured this will immediately be seen by the end user as both a: unobtrusive, and b: a link to my app's info (hooking the outside contact info with the internal info my app adds and keeps). The only problem? I have NO idea how to add an entry to the kABPersonInstantMessageProperty key. I have figured out how to add multi-value entries such as "Home Address" but when searching stack overflow, I come up with 4 (ONLY FOUR!) entries on questions regarding this key (kABPersonInstantMessageProperty).
A portion of my code for adding "Home Address" to a person's Address Book entry is below and I admit that I have no idea how to change this over to kABPersonInstantMessageProperty.
ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
NSMutableString *street = [NSMutableString stringWithFormat:#"%i",i];
[street appendString:#" Main Street"];
[addressDict setObject:[NSString stringWithString:street] forKey:(NSString *)kABPersonAddressStreetKey];
[addressDict setObject:#"San Jose" forKey:(NSString *)kABPersonAddressCityKey];
[addressDict setObject:#"CA" forKey:(NSString *)kABPersonAddressStateKey];
NSMutableString *zip = [NSMutableString stringWithString:#"95"];
[zip appendString:[NSString stringWithFormat:#"%00i",i]];
[addressDict setObject:zip forKey:(NSString *)kABPersonAddressZIPKey];
ABMultiValueAddValueAndLabel(address, addressDict, kABHomeLabel, NULL);
ABRecordSetValue(record, kABPersonAddressProperty, address, NULL);
// add the record
ABAddressBookAddRecord(addressBook, record, NULL);
Can someone help? I would appreciate it.
GOT IT!
This is so simple! Thanks to link text who had the same issue with lack of Google-able answers on this one. Thanks, Casey!
Whereas record is the ABPerson record and all the ABCreate stuff has already been done and i is the sequential record locator integer...
ABMutableMultiValueRef im = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *imDict = [[NSMutableDictionary alloc] init];
[imDict setObject:#"MyAppName" forKey:(NSString*)kABPersonInstantMessageServiceKey];
NSMutableString *iMID = [NSMutableString stringWithFormat:#"%i",i];
[iMID appendString:#"RL#MyAppDomain"];
[imDict setObject:iMID forKey:(NSString*)kABPersonInstantMessageUsernameKey];
ABMultiValueAddValueAndLabel(im, imDict, kABHomeLabel, NULL);
[imDict release];
ABRecordSetValue(record, kABPersonInstantMessageProperty, im, NULL);
My purpose is to process one image from image library, and save this image directly to one person record (i,e, "Tom" ), which is selected from AddressBook, I can see the new image replace Tom's previous image, but I can not save it to AddressBook.
I implemented delegate ABPeoplePickerNavigationControllerDelegate, and using following logics, but whatever I tried, modified records can not be saved to address books.
Any clues or suggest ? Thanks
ABAddressBookRef addressBook= ABAddressBookCreate();
CFErrorRef error = NULL;
BOOL wantToSaveChanges = YES;
ABRecordSetValue(person, kABPersonFirstNameProperty, #"Shi", &error);//, &error)
NSData * dataRef = UIImagePNGRepresentation(cellImage);
ABPersonSetImageData(person, dataRef, &error);
if (ABAddressBookHasUnsavedChanges(addressBook)) {\
NSLog(#"need to save ");
if (wantToSaveChanges) {
ABAddressBookSave(addressBook, &error);
} else {
ABAddressBookRevert(addressBook);
}
}else {
NSLog(#"no changes");
}
if (error != NULL) {/*... Handle error. ...*/
NSLog(#"error happened here " );
}
CFRelease(addressBook);
See:
Setting Address Book image for a contact doesn't seem to work
and
ABPersonSetImageData Only Altering the Contact Thumbnail and Not the Full Pic
Using the standard add record code I am getting a very strange error when setting properties other than FirstName and Organization (the first 2 lines work):
ABRecordSetValue(person, kABPersonFirstNameProperty, location.title , nil);
ABRecordSetValue(person, kABPersonOrganizationProperty, location.title , nil);
ABRecordSetValue(person, kABPersonPhoneProperty, [location telephone], nil);
ABRecordSetValue(person, kABPersonAddressStreetKey, [location addressLine1], nil);
ABRecordSetValue(person, kABPersonAddressCityKey, [location addressTownCity], nil);
ABRecordSetValue(person, kABPersonAddressZIPKey, [location addressPostcode], nil);
ABRecordSetValue(person, kABPersonAddressStateKey, [location addressCounty], nil);
FYI it isnt the difference between location.title and [location addressLine1] as I have tried setting kABPersonAddressStreetKey to location.title with the same problem
Even with this code I get a EXC_BAD_ACCESS
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();
ABRecordSetValue(person, kABPersonFirstNameProperty, location.title , nil);
ABRecordSetValue(person, kABPersonPhoneProperty, #"0208 1567890", nil);
ABAddressBookAddRecord(addressBook, person, nil);
ABAddressBookSave(addressBook, nil
Cause of the telephone line, I must be doing something stupid surely!?!?!?
Check the hints that are given in Finding EXC_BAD_ACCESS bugs in a Cocoa project.
Also useful in this case is the NSZombieEnabled feature as explained in Debugging Applications
Have you tried to debug to that specific line of code using the debugger to print out the value of location variable?
Or at least, log that variable's value to NSLog().
Edited:
In this case, I guess location variable do not respond to the selector methods.
Try to "Step Into" the line 3 to see what happens?
Ah it seems it is cause the telephone and address fields are multi value, I hadnt realised this
More details can be found
http://www.modelmetrics.com/tomgersic/iphone-programming-adding-a-contact-to-the-iphone-address-book/