Friday, 30 September 2011

Add New Contact in iPhone by Code


Add AddressBook Framework.

#import <AddressBook/AddressBook.h>


       ABRecordRef aRecord = ABPersonCreate();
       CFErrorRef  anError = NULL;
      
       ABRecordSetValue(aRecord,kABPersonOrganizationProperty,CFSTR("Sierra Tucson"), &anError);

       ABMutableMultiValueRef multiWebUrl = ABMultiValueCreateMutable(kABMultiStringPropertyType);
       ABMultiValueAddValueAndLabel(multiWebUrl, @"www.sierratucson.com", CFSTR("Web"), NULL);
       ABRecordSetValue(aRecord, kABPersonURLProperty, multiWebUrl, &anError);
       CFRelease(multiWebUrl);
      
      
       ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
       ABMultiValueAddValueAndLabel(multiPhone, @"8006245858", CFSTR("Phone"), NULL);
       ABMultiValueAddValueAndLabel(multiPhone, @"8006249001", CFSTR("Intake"), NULL);           
       ABRecordSetValue(aRecord, kABPersonPhoneProperty, multiPhone,nil);
      
      
       if (anError != NULL) {
          
           NSLog(@"error while creating..");
       }
      
       //ABAddressBookRef addressBook;
       CFErrorRef error = NULL;
       //addressBook = ABAddressBookCreate();
      
       BOOL isAdded = ABAddressBookAddRecord (addressBook,aRecord,&error);
      
       if(isAdded){
           NSLog(@"added..");
       }
       if (error != NULL) {
           NSLog(@"ABAddressBookAddRecord %@", error);
       }
       error = NULL;
      
       BOOL isSaved = ABAddressBookSave (addressBook,&error);
       if(isSaved)
       {
           NSLog(@"saved..");
       }
      
       if (error != NULL)
       {
           NSLog(@"ABAddressBookSave %@", error);
       }
      
       CFRelease(aRecord);
       CFRelease(multiPhone);
       CFRelease(addressBook);

No comments:

Post a Comment