实用知识:<AddressBookUI/AddressBookUI.h>(获取手机通讯录)

#import "ViewController.h"

// 提供联系人查询, 添加的UI界面
#import <AddressBookUI/AddressBookUI.h>

@interface ViewController () <UINavigationControllerDelegate, ABPeoplePickerNavigationControllerDelegate>

// 联系人查询控制器
@property (strong, nonatomic) ABPeoplePickerNavigationController *peoplePicker;

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 1. 创建
    self.peoplePicker = [[ABPeoplePickerNavigationController alloc] init];

    // 2. 配置
//    self.peoplePicker.delegate = self;
    // 注意: 代理属性是peoplePickerDelegate, delegate属性是从UINavigationController继承过来的
    self.peoplePicker.peoplePickerDelegate = self;

    // 3. 弹出
    [self presentViewController:self.peoplePicker animated:YES completion:nil];
}

#pragma mark - ABPeoplePickerNavigationControllerDelegate

// 点击了右上解的取消按钮时触发
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    NSLog(@"取消");
}

// 选中了某个联系人时触发
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    // 如果实现了该代理方法, 那么不会进入联系人详情界面
    NSLog(@"选中了联系人");

    /*================= 获取联系人的姓名 =================*/
    // ABRecordRef : 表示通讯录数据库当中的一条记录
    // C 语言的方法通过以自己为方法名开头, 通常第一个参数都Ref是填自己

    // person 实际上是 ABPerson, Property ID 定义在 ABPerson 里面
    // 凡是create, retain, copy, 都需要手动的去Release

    // CFTypeRef 是通用类型, 类似于 id
    CFStringRef firstname = ABRecordCopyValue(person, kABPersonFirstNameProperty);  // 名
    CFStringRef lastname = ABRecordCopyValue(person, kABPersonLastNameProperty);   // 姓

    /*================= CoreFoundation 与 Foundation 之间的转换 =================*/
    // 方式1: 直接转换, 不转移对象所有权 (负责retainCount引用计数)
//    NSString *firstNameString = (__bridge NSString *)(f\irstname);
//    NSLog(@"%@", firstNameString);
//    
//    CFRelease(firstname);

    // 方式2: 将非OC转换成OC, 转移对象所有权 (ARC)
//    NSString *firstNameString = CFBridgingRelease(firstname);
    // 两者等价
    NSString *firstNameString = (__bridge_transfer NSString *)firstname;
    NSString *lastNameString = CFBridgingRelease(lastname);
    NSLog(@"%@ : %@", firstNameString,  lastNameString);


    /*================= 获取联系人电话 =================*/
    // kABPersonPhoneProperty 得到的是 ABMultiValueRef 的值
    // ABMultiValueRef 可以理解成是一个包装 字典(Label - Value) 的 数组(多个电话)
    ABMultiValueRef multiValue = ABRecordCopyValue(person, kABPersonPhoneProperty);

    // 获取MultiValue的值的个数
    CFIndex index = ABMultiValueGetCount(multiValue);
    for (CFIndex i = 0; i < index; i++) {
        // 获取每一个索引对应的label与value
        CFStringRef label = ABMultiValueCopyLabelAtIndex(multiValue, i);
        // 电话号码也是 CFStringRef
        CFStringRef value = ABMultiValueCopyValueAtIndex(multiValue, i);

        NSLog(@"%@ : %@", CFBridgingRelease(label), CFBridgingRelease(value));
    }

    CFRelease(multiValue);
}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码,你可以根据自己的需要进行修改: HTML: ``` <h1>通讯录</h1> <button class="add-btn">添加联系人</button> <table> <thead> <tr> <th>姓名</th> <th>电话</th> <th>邮箱</th> <th>操作</th> </tr> </thead> <tbody> <!-- 数据渲染 --> </tbody> </table> <!-- 弹出层,用于添加或编辑联系人 --> <div id="modal" class="modal" style="display: none;"> <div class="modal-content"> <span class="close">×</span> <form> <div class="form-group"> <label for="name">姓名</label> <input type="text" id="name" name="name" required> </div> <div class="form-group"> <label for="phone">电话</label> <input type="text" id="phone" name="phone" required> </div> <div class="form-group"> <label for="email">邮箱</label> <input type="text" id="email" name="email" required> </div> <button id="save-btn">保存</button> <button id="cancel-btn">取消</button> </form> </div> </div> ``` CSS: ``` /* 弹出层样式 */ .modal { display: none; /* 默认隐藏 */ position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0, 0, 0, 0.4); /* 添加半透明背景 */ } .modal-content { background-color: #fefefe; margin: 10% auto; padding: 20px; border: 1px solid #888; width: 40%; } .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; } /* 表格样式 */ table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; } th { background-color: #4CAF50; color: white; } tr:nth-child(even) { background-color: #f2f2f2; } tr:hover { background-color: #ddd; } /* 按钮样式 */ button { background-color: #4CAF50; color: white; border: none; padding: 10px; margin: 5px; cursor: pointer; } button:hover { background-color: #3e8e41; } /* 表单样式 */ .form-group { margin-bottom: 10px; } label { display: block; margin-bottom: 5px; font-weight: bold; } input[type=text] { width: 100%; padding: 10px; border-radius: 3px; border: 1px solid #ccc; box-sizing: border-box; } button#save-btn { background-color: #4CAF50; color: white; border: none; padding: 10px 20px; margin-top: 10px; cursor: pointer; } button#cancel-btn { background-color: #ccc; color: white; border: none; padding: 10px 20px; margin-top: 10px; cursor: pointer; } button#save-btn:hover { background-color: #3e8e41; } button#cancel-btn:hover { background-color: #bbb; } ``` JavaScript: ``` // 获取元素 var modal = document.getElementById("modal"); var addBtn = document.querySelector(".add-btn"); var closeBtn = document.querySelector(".close"); // 点击添加联系人按钮,弹出添加联系人窗口 addBtn.addEventListener("click", function() { modal.style.display = "block"; }); // 点击关闭按钮或者弹出层之外的区域,关闭弹出层 closeBtn.addEventListener("click", function() { modal.style.display = "none"; }); window.addEventListener("click", function(event) { if (event.target == modal) { modal.style.display = "none"; } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值