Tuesday, January 03, 2012

iOS: Adding Contacts Using AddressBook Framework

    ABRecordRef aRecord = ABPersonCreate();
   
    CFErrorRef  anError = NULL;
   
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty,
                      CFSTR("MAHES"), &anError);
    ABRecordSetValue(aRecord, kABPersonLastNameProperty,
                     CFSTR("CHELLAIAH"), &anError);
    ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiPhone, CFSTR("9751050790"), kABPersonPhoneMobileLabel, NULL); 
    ABRecordSetValue(aRecord, kABPersonPhoneProperty, multiPhone,nil);
    CFRelease(multiPhone);
   
    if (anError != NULL) {
       
        NSLog(@"error while creating..");
    }
   
    CFStringRef firstName, lastName;
    firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
    lastName  = ABRecordCopyValue(aRecord, kABPersonLastNameProperty);
   
   
    CFErrorRef error = NULL;
    ABAddressBookRef 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..");
        [self handleCall];
    }
   
    if (error != NULL) {
        NSLog(@"ABAddressBookSave %@", error);
    }
   
    CFRelease(aRecord);
    CFRelease(firstName);
    CFRelease(lastName);
    CFRelease(addressBook);

No comments:

Swift Operators - Basic Part 3 (Range operators in swift)

Range Operators: Closed Range operators  Half-Open Range Operators One-Sided Ranges Closed Range Operators:  a...b It defines...