Monday, July 16, 2012

Add new contact in address book



    CFErrorRef
 error = NULL;
    ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
    ABRecordRef newPerson = ABPersonCreate();
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty,@"Davis", &error);
    ABRecordSetValue(newPerson, kABPersonLastNameProperty@"Scott", &error);
    
    
    //Add my phone number
    ABMutableMultiValueRef PhoneVar = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(PhoneVar,valueForKey:@"6176352360"kABPersonPhoneMainLabelNULL);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, PhoneVar,nil);
    CFRelease(PhoneVar);
    
    
    //Add my email address
    ABMutableMultiValueRef EmailVar = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(EmailVar,@"d.scott@hotmail.com" , kABWorkLabelNULL);
    ABRecordSetValue(newPerson, kABPersonEmailProperty, EmailVar,nil);
    CFRelease(EmailVar);
    
   
    //Add my mailing address
    ABMutableMultiValueRef Address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDict = [[NSMutableDictionary allocinit];
    [addressDictionary setObject:streetString forKey:(NSString *) kABPersonAddressStreetKey];
    [addressDictionary setObject:@"Boston" forKey:(NSString *)kABPersonAddressCityKey];
    [addressDictionary setObject:@"02124" forKey:(NSString *)kABPersonAddressZIPKey];
    [addressDictionary setObject:@"US" forKey:(NSString *)kABPersonAddressCountryKey];
    ABMultiValueAddValueAndLabel(Address, addressDict, kABWorkLabelNULL);
    ABRecordSetValue(newPerson, kABPersonAddressProperty, Address,&error);
    CFRelease(Address);
    
    //Finally saving the contact in the address book
    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
    ABAddressBookSave(iPhoneAddressBook, &error);
    if (error != NULL
    {
        NSLog(@"Saving contact failed.");
    }


    NSLog(@"Contact saved.");

No comments:

Post a Comment