Monday, June 25, 2012

iOS - Add Contact to AddressBook - Sample code




Add AddressBook.Framework to the Project

#import <AddressBook/AddressBook.h>

-(NSUInteger) copyContactToAB{

NSUInteger addressbookId = 0;

    UIImage *imgContact = nil;//[UIImage imageNamed:@"test.png"];
    NSString *companyName = @"Test";
    
    NSString *firstName= @"Hanoch";
    NSString *lastName= @"J";
    NSString *workPhone = nil;
    NSString *workFax = nil;
    NSString *mobile = nil;
    NSString *homePhone = nil;
    NSString *workEmail = nil;
    
    
    //work address
    NSString *address1_street = nil;
    NSString *address1_city = nil;
    NSString *address1_state = nil;
    NSString *address1_postalcode = nil;
    NSString *address1_country = nil;
    
    //home address
    NSString *address2_street = nil;
    NSString *address2_city = nil;
    NSString *address2_state = nil;
    NSString *address2_postalcode = nil;
    NSString *address2_country = nil;
    
    
    ABRecordRef aRecord = ABPersonCreate();
    CFErrorRef  anError = NULL;
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty, firstName, &anError);
    ABRecordSetValue(aRecord, kABPersonLastNameProperty, lastName, &anError);
    if(companyName){
        ABRecordSetValue(aRecord, kABPersonOrganizationProperty, companyName, &anError);
    }
    
    
    
    
    
    if (anError != NULL) {
        
        MELog(@"error while creating..%@", anError);
    }
    
    if(imgContact){
        
        
        NSData *data = UIImagePNGRepresentation(imgContact);
        if(ABPersonSetImageData(aRecord, (CFDataRef)data, &anError)){
            
            
        }
        
        
    }
    
    
    //(@"adding email");
    if(workEmail){
        ABMutableMultiValueRef emails = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emails,  workEmail, kABWorkLabel, NULL);
        
        ABRecordSetValue(aRecord, kABPersonEmailProperty, emails, &anError);
        
        CFRelease(emails);
    }
    
    //(@"adding phonee");
    ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    
    
    if(workPhone) ABMultiValueAddValueAndLabel(multi, workPhone, kABWorkLabel, NULL) ;
    if(workFax) ABMultiValueAddValueAndLabel(multi, workFax, kABPersonPhoneWorkFAXLabel,NULL) ;//&&
    if(mobile) ABMultiValueAddValueAndLabel(multi, mobile, kABPersonPhoneMobileLabel,NULL);
    if(homePhone) ABMultiValueAddValueAndLabel(multi, homePhone, kABHomeLabel,NULL);
    
    if(workPhone || workFax || mobile || homePhone){
        
        ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &anError);
        if (anError != NULL) {   MELog(@"error while creating ABMutableMultiValueRef..%@", anError); }
        
    }
    
    CFRelease(multi);
    
    
    
    //add work adress
    ABMutableMultiValueRef multiAddress1 = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    
    NSMutableDictionary *addressDictionary1 = [[NSMutableDictionary alloc] init];
    if(address1_street)  [addressDictionary1 setObject:address1_street forKey:(NSString *) kABPersonAddressStreetKey];
    if(address1_city)  [addressDictionary1 setObject:address1_city forKey:(NSString *)kABPersonAddressCityKey];
    if(address1_state)  [addressDictionary1 setObject:address1_state forKey:(NSString *)kABPersonAddressStateKey];
    if(address1_postalcode) [addressDictionary1 setObject:address1_postalcode forKey:(NSString *)kABPersonAddressZIPKey];
    if(address1_country) [addressDictionary1 setObject:address1_country forKey:(NSString *)kABPersonAddressCountryKey];
    //[addressDictionary setObject:@"us" forKey:(NSString *)kABPersonAddressCountryCodeKey];
    
    bool didAdd = NO;
    
    if([addressDictionary1 count] > 0){
        didAdd = ABMultiValueAddValueAndLabel(multiAddress1, addressDictionary1, kABWorkLabel, NULL);
    }
    
    [addressDictionary1 release];
    
    //add home address
    
    NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init];
    if(address2_street)  [addressDictionary2 setObject:address2_street forKey:(NSString *) kABPersonAddressStreetKey];
    if(address2_city)  [addressDictionary2 setObject:address2_city forKey:(NSString *)kABPersonAddressCityKey];
    if(address2_state)  [addressDictionary2 setObject:address2_state forKey:(NSString *)kABPersonAddressStateKey];
    if(address2_postalcode) [addressDictionary2 setObject:address2_postalcode forKey:(NSString *)kABPersonAddressZIPKey];
    if(address2_country)  [addressDictionary2 setObject:address2_country forKey:(NSString *)kABPersonAddressCountryKey];
    //[addressDictionary setObject:@"us" forKey:(NSString *)kABPersonAddressCountryCodeKey];
    
    if([addressDictionary2 count] > 0 ){
        didAdd = ABMultiValueAddValueAndLabel(multiAddress1, addressDictionary2, kABHomeLabel, NULL);
        
        
    }
    if(didAdd){
        //(@"home address added");
        anError = NULL;
        ABRecordSetValue(aRecord, kABPersonAddressProperty, multiAddress1, &anError);
        if (anError != NULL) {   MELog(@"error while creating home address..%@", anError); }
    }
    
    CFRelease(multiAddress1);
    [addressDictionary2 release];
    addressDictionary2 = nil;
    
    
    ////////////
    
    ABAddressBookRef addressBook;
    CFErrorRef error = NULL;
    addressBook = ABAddressBookCreate();
    
    /*BOOL isAdded =*/ ABAddressBookAddRecord (addressBook, aRecord, &error);
    

    if (error != NULL) {
        
        MELog(@"ABAddressBookAddRecord %@", error);
    }
    error = NULL;
    
    
    
    if(ABAddressBookSave ( addressBook,  &error)){
        
        
        addressbookId =  ABRecordGetRecordID (aRecord);
        
    }
    
    if (error != NULL) {
        MELog(@"ABAddressBookSave %@", error);
    }
    
    CFRelease(aRecord);

    CFRelease(addressBook);
    
    
return addressbookId;

}
Thanks to http://www.simplebits.com/cgi-bin/simplecode.pl?mode=process to format the above code