简体   繁体   中英

How to add a contact from my app to the iOS address book

I would like to add the ability to save a contact (phone number, name, address and email) in the iOS address book by clicking on a button in my application! How can I do this?

Its so easy and follows below steps,

First, you have import following frameworks,

#import <AddressBook/AddressBook.h>

#import <AddressBookUI/AddressBookUI.h

Second, write the following code in your button action and modify the code according to your needs,

        NSString * addressString1 = [appDelegate getCurrentSummary];

        NSString * addressString2 = [appDelegate getCurrentTubeImage];

        NSString * cityName = [appDelegate getCurrentcheckValue];

        NSString * stateName = [appDelegate getCurrentTubeUrl];

        NSString * postal = [appDelegate getCurrentViews];

        NSString * emailString = [appDelegate getCurrentLink];

        NSString * phoneNumber = [appDelegate getCurrentTitle];

        NSString * prefName = [appDelegate getCurrentAuthor];

        ABAddressBookRef libroDirec = ABAddressBookCreate();

        ABRecordRef persona = ABPersonCreate();

        ABRecordSetValue(persona, kABPersonFirstNameProperty, prefName, nil);

            ABMutableMultiValueRef multiHome = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

            NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

            NSString *homeStreetAddress=[addressString1 stringByAppendingString:addressString2];

            [addressDictionary setObject:homeStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];

            [addressDictionary setObject:cityName forKey:(NSString *)kABPersonAddressCityKey];

            [addressDictionary setObject:stateName forKey:(NSString *)kABPersonAddressStateKey];

            [addressDictionary setObject:postal forKey:(NSString *)kABPersonAddressZIPKey];

            bool didAddHome = ABMultiValueAddValueAndLabel(multiHome, addressDictionary, kABHomeLabel, NULL);

            if(didAddHome)
            {
                ABRecordSetValue(persona, kABPersonAddressProperty, multiHome, NULL);

                NSLog(@"Address saved.....");
            }

            [addressDictionary release];

        //##############################################################################

        ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);

        bool didAddPhone = ABMultiValueAddValueAndLabel(multiPhone, phoneNumber, kABPersonPhoneMobileLabel, NULL);

        if(didAddPhone){

            ABRecordSetValue(persona, kABPersonPhoneProperty, multiPhone,nil);

            NSLog(@"Phone Number saved......");

        }

        CFRelease(multiPhone);

        //##############################################################################

        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);

        bool didAddEmail = ABMultiValueAddValueAndLabel(emailMultiValue, emailString, kABOtherLabel, NULL);

        if(didAddEmail){

        ABRecordSetValue(persona, kABPersonEmailProperty, emailMultiValue, nil); 

        NSLog(@"Email saved......");
        }

        CFRelease(emailMultiValue);

        //##############################################################################

        ABAddressBookAddRecord(libroDirec, persona, nil);

        CFRelease(persona);

        ABAddressBookSave(libroDirec, nil);

        CFRelease(libroDirec);

        NSString * errorString = [NSString stringWithFormat:@"Information are saved into Contact"];

         UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"New Contact Info" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

         [errorAlert show];

        [errorAlert release];

}

I hope it will help you!

Here is Code to Add Contact with Person Image in Iphone Address Book

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();

// Add Person Image

NSData *dataRef = UIImagePNGRepresentation(personImageView.image);
ABPersonSetImageData(person, (CFDataRef)dataRef, nil);

CFErrorRef  anError = NULL;

// Full Name, Company Name, Designation Property

ABRecordSetValue(person,kABPersonFirstNameProperty,full_name,&anError);
ABRecordSetValue(person, kABPersonOrganizationProperty, companyLabel, &anError);
ABRecordSetValue(person, kABPersonJobTitleProperty, designation, &anError);

// Multi value Address Property

ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);'
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

[addressDictionary setObject:@"Chicago" forKey:(NSString *)kABPersonAddressCityKey];
[addressDictionary setObject:@"IL" forKey:(NSString *)kABPersonAddressStateKey];
[addressDictionary setObject:@"60654" forKey:(NSString *)kABPersonAddressZIPKey];

ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, (CFStringRef)@"Address", NULL);
ABRecordSetValue(person, kABPersonAddressProperty, multiAddress,&anError);
    CFRelease(multiAddress);

ABMutableMultiValueRef websiteMultiValue = ABMultiValueCreateMutable(kABPersonURLProperty);
ABMultiValueAddValueAndLabel(websiteMultiValue, web_URL, (CFStringRef)@"Website", NULL);
ABRecordSetValue(person, kABPersonURLProperty, websiteMultiValue, &anError);


ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
ABMultiValueAddValueAndLabel(emailMultiValue, email, (CFStringRef)@"Email", NULL);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, &anError);

ABAddressBookAddRecord(addressBook, person, &anError);

If anyone is looking for the way to add address details (street, city, state, zip) in Swift, it's not well documented on Apple's web site. Here's the way I did it (only address details shown in the example)...

var addressDictionary: [String:String] = [
    kABPersonAddressStreetKey as String: Contact.address_1,
    kABPersonAddressCityKey as String: Contact.city,
    kABPersonAddressStateKey as String: Contact.state,
    kABPersonAddressZIPKey as String: Contact.zip
]

let multiWork: ABMutableMultiValue = 
  ABMultiValueCreateMutable(
    ABPropertyType(kABMultiDictionaryPropertyType)).takeRetainedValue()
ABMultiValueAddValueAndLabel(multiWork, addressDictionary, kABWorkLabel, nil)
ABRecordSetValue(person, kABPersonAddressProperty, multiWork, nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM