iOS开发- 添加地址从通讯录中选择添加(firstName,lastName真机为nil,模拟器正常)

这里写图片描述
如图所示,即为博主今天要说的功能,想必很多app都有出现,大家也见过类似的功能,网上的代码也是属于烂大街的东西,随便一搜就有一箩筐,这是博主的代码:

在使用下面代码之前,需要先引入:
#import <AddressBookUI/ABPeoplePickerNavigationController.h>
#import <AddressBook/ABPerson.h>
#import <AddressBookUI/ABPersonViewController.h>

遵守ABPeoplePickerNavigationControllerDelegate协议

#pragma mark - addReceiverBtnAction
- (void)addReceiverBtnAction
{
//点击➕触发事件,记得plist设置允许访问手机通讯录,不多说了。
    ABPeoplePickerNavigationController *nav = [[ABPeoplePickerNavigationController alloc] init];
    nav.peoplePickerDelegate = self;
    //这个方法是在iOS8.0时才出现的,在其后要增加if中的代码,否则点击联系人后会直接dismiss,错误信息如下:
    //[App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction

    if([[UIDevice currentDevice].systemVersion floatValue]>=8.0){
        nav.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
    }
    [self presentViewController:nav animated:YES completion:nil];
}

//此处为选择通讯录联系人的代理回调
#pragma mark - ABPeoplePickerNavigationControllerDelegate
//取消选择
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
    ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
    long index = ABMultiValueGetIndexForIdentifier(phone,identifier);
    NSString *phoneNO = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, index);

    //有些手机号码带@“+86”,所以从第三位取
    if ([phoneNO hasPrefix:@"+"]) {
        phoneNO = [phoneNO substringFromIndex:3];
    }
    //去除手机号中间的@“-”
    phoneNO = [phoneNO stringByReplacingOccurrencesOfString:@"-" withString:@""];

    //这里的firstName和lastName指的是姓氏和名字,添加到通讯录时会有,但是这里有一个问题,博主在iOS10.3的系统上模拟器添加成功,但是在真机上这两个参数为nil,很匪夷所思的问题,好在有另一个方法,获取全名,如下anFullName:
    NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    CFStringRef anFullName = ABRecordCopyCompositeName(person);
    UITextField *address_contentField = [self.view viewWithTag:100];
    UITextField *address_contentField1 = [self.view viewWithTag:101];
    NSLog(@"%@%@---%@",firstName,lastName,anFullName);

    if (phoneNO) {
        address_contentField1.text = phoneNO;
        addressMobile = phoneNO;
    }
    else
        address_contentField1.text = @"";
    if (anFullName) {
        address_contentField.text = [NSString stringWithFormat:@"%@",anFullName];
        addressName = (__bridge NSString *)(anFullName);

    }
    else
        address_contentField.text = @"";
}

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
{
    ABPersonViewController *personViewController = [[ABPersonViewController alloc] init];
    personViewController.displayedPerson = person;
    [peoplePicker pushViewController:personViewController animated:YES];
}


到这里并没有结束,除了获取这些个参数外,也许满足了大部分人的需求,但是你不知道的是,我们还可以获取一些别的东西,我们知道通讯录并不只是姓名和电话,还有组织,工作,部门之类的信息,如果设置齐全,是可以一并获取的,方法参考下面:
// Property keys
AB_EXTERN const ABPropertyID kABPersonFirstNameProperty AB_DEPRECATED("use CNContact.givenName");                               // First name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonLastNameProperty AB_DEPRECATED("use CNContact.familyName");                               // Last name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonMiddleNameProperty AB_DEPRECATED("use CNContact.middleName");                             // Middle name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonPrefixProperty AB_DEPRECATED("use CNContact.namePrefix");                                 // Prefix ("Sir" "Duke" "General") - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonSuffixProperty AB_DEPRECATED("use CNContact.nameSuffix");                                 // Suffix ("Jr." "Sr." "III") - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonNicknameProperty AB_DEPRECATED("use CNContact.nickname");                                 // Nickname - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonFirstNamePhoneticProperty AB_DEPRECATED("use CNContact.phoneticGivenName");               // First name Phonetic - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonLastNamePhoneticProperty AB_DEPRECATED("use CNContact.phoneticFamilyName");               // Last name Phonetic - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonMiddleNamePhoneticProperty AB_DEPRECATED("use CNContact.phoneticMiddleName");             // Middle name Phonetic - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonOrganizationProperty AB_DEPRECATED("use CNContact.organizationName");                     // Company name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonDepartmentProperty AB_DEPRECATED("use CNContact.departmentName");                         // Department name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonJobTitleProperty AB_DEPRECATED("use CNContact.jobTitle");                                 // Job Title - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonEmailProperty AB_DEPRECATED("use CNContact.emailAddresses");                              // Email(s) - kABMultiStringPropertyType
AB_EXTERN const ABPropertyID kABPersonBirthdayProperty AB_DEPRECATED("use CNContact.birthday");                                 // Birthday associated with this person - kABDateTimePropertyType
AB_EXTERN const ABPropertyID kABPersonNoteProperty AB_DEPRECATED("use CNContact.note");                                         // Note - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonCreationDateProperty AB_DEPRECATED("");                                                   // Creation Date (when first saved)
AB_EXTERN const ABPropertyID kABPersonModificationDateProperty AB_DEPRECATED("");                                               // Last saved date

// Addresses
AB_EXTERN const ABPropertyID kABPersonAddressProperty AB_DEPRECATED("use CNContact.postalAddresses");                           // Street address - kABMultiDictionaryPropertyType

//这些都是系统自带的一些属性,学习的小伙伴可以一个个替换来查看。

这篇博文就到此结束了,要说没什么难度,都是苹果API给出的方法,套路罢了,用过就知道了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
很抱歉,我无法提供C语言代码。但是,我可以向您介绍如何使用Node.js模块来创建、读取、更新和删除macOS上的用户联系人数据库。 1. 创建通讯录 要创建通讯录,您需要使用Node.js模块node-mac-contacts。首先,您需要安装该模块。在终端运行以下命令: ``` npm i node-mac-contacts ``` 安装完成后,您可以使用以下代码创建一个联系人: ```javascript const contacts = require('node-mac-contacts'); const newContact = { firstName: 'John', lastName: 'Doe', email: '[email protected]', phone: '123-456-7890' }; contacts.addContact(newContact, (err, contact) => { if (err) { console.log(err); } else { console.log('Contact created:', contact); } }); ``` 2. 显示通讯录 要显示通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); contacts.getAllContacts((err, contacts) => { if (err) { console.log(err); } else { console.log('Contacts:', contacts); } }); ``` 3. 查询通讯录 要查询通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); const query = { firstName: 'John', lastName: 'Doe' }; contacts.findContacts(query, (err, contacts) => { if (err) { console.log(err); } else { console.log('Contacts:', contacts); } }); ``` 4. 修改通讯录 要修改通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); const query = { firstName: 'John', lastName: 'Doe' }; const update = { email: '[email protected]' }; contacts.updateContact(query, update, (err, contact) => { if (err) { console.log(err); } else { console.log('Contact updated:', contact); } }); ``` 5. 添加通讯录添加通讯录,您可以使用第1步的代码。 6. 删除通讯录 要删除通讯录,您可以使用以下代码: ```javascript const contacts = require('node-mac-contacts'); const query = { firstName: 'John', lastName: 'Doe' }; contacts.deleteContact(query, (err) => { if (err) { console.log(err); } else { console.log('Contact deleted'); } }); ``` 7. 退出系统 要退出系统,您可以使用以下代码: ```javascript process.exit(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值