Friday, April 27, 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", kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhonePropertyPhoneVar,nil);
    CFRelease(PhoneVar);
    
    
    //Add my email address
    ABMutableMultiValueRef EmailVar = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(EmailVar,@"d.scott@hotmail.com" , kABWorkLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonEmailPropertyEmailVar,nil);
    CFRelease(EmailVar);
    
   
    //Add my mailing address
    ABMutableMultiValueRef Address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
    [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(AddressaddressDict, kABWorkLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonAddressPropertyAddress,&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