iOS-----使用AddressBook新增連絡人...,iosaddressbook

來源:互聯網
上載者:User

iOS-----使用AddressBook新增連絡人...,iosaddressbook
使用AddressBook新增連絡人...

 

新增連絡人...的步驟如下:

1

建立ABAddressBookRef,這就得到了對地址簿的引用。

2

調用ABPersonCreate()函數建立一個空的ABRecordRef,得到一條空的連絡人記錄

3

根據需要為ABRecordRef設定屬性值。

        只設定姓氏、名字等簡單資訊,則直接調用ABRecordSetValue()函數為ABRecordRef的指定屬性設定屬性值即可.

        設定電話號碼、電子郵件等可以指定label的屬性,需要先建立一個ABMutableMultiValueRef,

然後調用ABMultiValueAddValueAndLabel()函數向ABMutableMultiValueRef中添加多個值,最後才能調用ABRecordSetValue()函數為ABRecordRef的指定屬性設定屬性值.

4

調用ABAddressBookAddRecord()函數將指定ABRecordRef記錄添加到地址簿中.

5

調用ABAddressBookSave()函數將刪除操作儲存到底層地址簿中.

程式碼片段

// 特此說明用的main. storyboard@implementation ViewController- (void)viewDidLoad{  [super viewDidLoad];}- (IBAction)add:(id)sender{// 名  NSString *firstName = self.firstnameField.text;  // 姓  NSString *lastName = self.lastnameField.text;  NSString *homePhone = self.homePhoneField.text;// 有線電話  NSString* mobilePhone = self.mobilePhoneField.text :// 手機NSString* workMail = self.workMailField.text ;NSString* privateMail = self. privateMailField.text ;NSString* country = self.countryField.text ;NSString* state = self. stateField.text ;CFErrorRef error = nil;// 建立ABAddressBook,該函數的第1個參數暫時並未使用,直接傳入NULL即可.ABAddressBookRef  ab = ABAddressBookCreateWithOptions(NULL, &error);if(!error){  // 請求訪問使用者地址簿ABAddressBookAccessWithCompletion(ab, ^(bool  granted, CFErrorRef  error){  // 如果使用者允許訪問地址簿if(granted){// 建立一條新的記錄ABRecordRef  rec = ABPersonCreate();// 為rec的kABPersonFirstNameProperty(名字)屬性設定值ABRecordSetValue(rec, kABPersonFirstNameProperty, (__bridge CFStringRef)firstName, NULL);// 為rec的kABPresonLastNameProperty(姓氏)屬性設定值ABRecordSetValue(rec, kABPersonLastNameProperty, (__bridge CFStringRef)lastName, NULL);// 建立ABMutableMultiValueRef來管理多個電話號碼ABMutableMultiValueRef  phoneValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);// 為家庭的電話號碼添加labelABMultiValueAddValueAndLabel(phoneValue , (__bridge  CFTypeRef)homePhone),  kABHomeLabel,  NULL);// 為移動的電話號碼添加labelABMultiValueAddValueAndLabel(phoneValue , (__bridge  CFTypeRef)mobilePhone),  kABPersonPhoneMobileLabel,  NULL);     // 為rec的kABPersonPhoneProperty(電話)屬性設定值ABRecordSetValue(rec, kABPersonPhoneProperty,  phoneValue, NULL);// 建立ABMutableMultiValueRef來管理多個電子郵件ABMutableMultiValueRef  mailValue = ABMultiValueCreateMutable(kABPersonEmailProperty);// 為工作的電子郵件添加labelABMultiValueAddValueAndLabel(mailValue , (__bridge  CFStringRef)workMail,   (__bridge  CFStringRef)@”工作”,  NULL);// 為私人的電子郵件添加labelABMultiValueAddValueAndLabel(mailValue , (__bridge  CFStringRef)privateMail,   (__bridge  CFStringRef)@”私人”,  NULL);// 為rec的kABPersonEmailProperty(電子郵件)屬性設定值ABRecordSetValue(rec, kABPersonMailProperty,  mailValue, NULL);// 建立ABMutableMultiValueRef來管理多個地址ABMutableMultiValueRef addrValue = ABMultiValueCreateMutable(kABPersonAddressProperty);// 建立一條新的ABRecordRef記錄,這條記錄不包含任何屬性NSDictionary *addrDict = [NSDictionary dictionaryWithObjectsAndKeys:country, kABPersonAddressCountryKey,  state, kABPersonAddressStateKey, nil];// 為住址的地址添加labelABMultiValueAddValueAndLabel(addrValue, (__bridge CFTypeRef)addrDict, (__bridge CFStringRef)@”地址”,  NULL);//  為rec的kABPersonAddressProperty(地址)屬性設定值ABRecordSetValue(rec, kABPersonAddressProperty, addrValue, NULL);BOOL result = ABAddressBookAddRecord(ab,  rec,  NULL);   if(result)   {      // 將程式所做的修改儲存到地址簿中,如果儲存成功      if(ABAddressBookSave(ab, NULL))     {       [self showAlert:@”成功添加新的連絡人”];     }     else    {       [self showAlert:@”添加失敗”];     }    }   } })}- (IBAction)finishEdit:(id)sender{    [sender  resignFirstResponder];}- (void)showAlert:(NSString *)msg{   // 使用UIAlertView顯示msg資訊  [ [ [UIAlertView alloc]  initWithTitle:@” 提 示 ”                                 message:msg                                delegate:nil                         cancelButtonTitle:@”確定”                         otherButtonTitles:nil] show ];}@end// 注釋:因為iOS系統要求地址必須是{國家:國家值, 省份:省份值, 城市:城市值}這種NSDictionary對象,所以建立了一個NSDictionary對象作為地址值

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.