Read/Parse Data from vCard - iphone

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.

Related

How to get contacts with Email from AddressBook in iOS?

In my app I want to get the names and emails of the phone contacts who has emails then list them in a tableview.
Is it possible to create user objects with the name and email fields which are filled by the address book of the phone then put those object in a tableview?
Any clue will be appreciated.
The approach you need to follow is :
First you need to get the reference of the AddressBook and then get all the references of contacts in an array. And then you need to enumerate the array and check whether their email property is nil, if not nil, add that contact to another array and then use that array as a datasource for the tableView :)
If you want, I can provide you with the code for checking the email entry for contact in addressBook and then filter those contacts.
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
for (int i=0;i < nPeople;i++) {
NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
//For username and surname
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty);
CFStringRef firstName, lastName;
firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
[dOfPerson setObject:[NSString stringWithFormat:#"%# %#", firstName, lastName] forKey:#"name"];
//For Email ids
ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty);
if(ABMultiValueGetCount(eMail) > 0) {
[dOfPerson setObject:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:#"email"];
}
With the help of you will get to know, whether the contact has an email or not, if it has, then you can add that contact reference to other array :) Hope it helps :)
You can follow this link - http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Introduction.html
ABAddressBook class lets you to access the address book data. And you can create your own custom object to these.
kABPersonEmailProperty lets you get the email property of an ABPerson

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

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/

Adding phone number to iphone contact - but NOT replacing!

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