如何将我的应用程序中的联系人添加到iOS通讯录中 [英] How to add a contact from my app to the iOS address book

查看:154
本文介绍了如何将我的应用程序中的联系人添加到iOS通讯录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过点击我的应用程序中的按钮添加在iOS地址簿中保存联系人(电话号码,姓名,地址和电子邮件)的功能!我怎么能这样做?

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!

这篇关于如何将我的应用程序中的联系人添加到iOS通讯录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆