Adding phone number to iphone contact - but NOT replacing! - iphone

I am trying to replicate the behavior that the iphone that happens when you tap and hold on a phone number link in a text field you get a menu "create new contact" and "add to existing contact". I have this working except for one thing. In the "add to existing" apple implementation if there is already a home contact, it just adds another one. It doesn't REPLACE it. So you can have multiple home phone numbers.
In my implementation it replaces it. So how do I do a not-destructive phone number add?
Here is my code:
+(void)updatePhone:(ABRecordRef)person phone:(NSString*)phone{
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, phone, kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
}
any ideas?

Did you try querying for the existing phone numbers, and then adding your new one?
Something like this (code not tested):
+(void)updatePhone:(ABRecordRef)person phone:(NSString*)phone{
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutableCopy (ABRecordCopyValue(person, kABPersonPhoneProperty));
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, phone, kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
}

Related

Read/Parse Data from vCard

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.

Add contact from iPhone application bugg

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

programmatically adding contact in iphone contact list

I want to programmatically add contact to iphone contact list without using ABNewPersonViewController. I would simply like to pass parameters and want the ABRecordRef as result. Is this possible?
ABGroupAddMember (group, contact, nil) ;
ABAddressBookSave(inAddressBook, nil) ;
After adding the contact save the address book.
bool ABAddressBookAddRecord (
ABAddressBookRef addressBook,
ABRecordRef record,
CFErrorRef *error
);
https://developer.apple.com/documentation/addressbook

How to save image to iphone's AddressBook using SDK API?

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

Add contact throws EXC_BAD_ACCESS exception for most properties (ABRecord)

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/