Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

iphone - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...