Add contact from iPhone application bugg - iphone

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

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.

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

contact notes are not transferred when exporting/importing via the built-in vCard representation methods

Using the ABPersonCreateVCardRepresentationWithPeople method for export, and the ABPersonCreatePeopleInSourceWithVCardRepresentation for import, I have successfully transfered contact data between devices. However, the data in the contact's "notes" field isn't transfered.
Here's my export function:
+(NSData*)exportContactsToVcard:(NSArray*)contacts
{
NSMutableArray *people = [NSMutableArray arrayWithCapacity:contacts.count];
ABAddressBookRef ab = ABAddressBookCreate();
for (Contact *contact in contacts)
{
ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,contact.contactId);
[people addObject:(__bridge id)person];
}
NSData *vCard = (__bridge NSData*)ABPersonCreateVCardRepresentationWithPeople((__bridge CFArrayRef) people);
return vCard;
}
and part of my import function:
+(NSArray*)importContactsFromVcardData:(NSData*)vcardData
{
NSMutableArray *addedContactIds = [NSMutableArray array];
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(addressBook);
NSArray *createdPeople = (__bridge_transfer NSArray*)ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource,(__bridge CFDataRef)vcardData);
CFErrorRef error = NULL;
for (id person in createdPeople)
{
error = NULL;
ABRecordRef personRecord = (__bridge ABRecordRef)person;
NSString *notes = (__bridge NSString *)ABRecordCopyValue(personRecord, kABPersonNoteProperty);
In the last line, notes is always nil, even if the contact had notes before it was exported. All the other standard contact fields seem to be in place.
For example, if I replace the last line with:
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(personRecord, kABPersonFirstNameProperty);
the firstName string will hold the contact's first name.
Any idea how I can work around this, and get the contact notes?
Thanks.
For testing purposes, you can export a vCard from Address Book. Then drag it to TextEdit to look at the various fields.
At the bottom you'll find something like this:
NOTE:This is a note!
Also, see this link for info on the vCard format:
http://en.wikipedia.org/wiki/VCard
ABPersonCreateVCardRepresentationWithPeople will only transfer the important contact details of a vCard...thats my identifying in much tests with the vCard and the iOS import-export function. It has many mistakes, e.g. if you try to import it with social networks like facebook, it won't do it with (just look in my other thread THREADLINK )
But if you try to add some vCard Information over ABNewPersonViewController, it will work perfectly.

Storing a record locator in the user's Address Book kABPersonInstantMessageProperty field

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

EXC_BAD_ACCESS when executing ABAddressBookSave !

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!