获取通讯录-RHAddressBook(swift)

获取通讯录-RHAddressBook(swift)

  1. 集成框架
    1. 将整个工程拖入项目
    2. 添加工程依赖
    Build Phases -> Target Dependencies -> +
    3. 添加链接项
    Build Settings -> Other Linker Flags -> -ObjC -all_load

  2. 代码实现

    let ab = RHAddressBook()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // 1. 判断授权状态, 请求授权
        let status = RHAddressBook.authorizationStatus()
        if status == RHAuthorizationStatus.NotDetermined {
    
            ab.requestAuthorizationWithCompletion({ (granted: Bool, error: NSError!) in
                if granted {
                    print("授权成功")
                }
            })
        }
    }
    
    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
    
        // 0 . 再次验证授权状态
        // 1. 判断授权状态, 请求授权
        let status = RHAddressBook.authorizationStatus()
        if status != RHAuthorizationStatus.Authorized {
            print("没有授权")
            return
        }
    
        // 2. 获取所有联系人信息
        let peoples = ab.people as! [RHPerson]
    
        for people in peoples {
            let firstName = people.firstName
            let lastName = people.lastName
    
            print(firstName, lastName)
    
            // 取出电话号码(多值属性)
            let phones: RHMultiValue! = people.phoneNumbers
    
            // 遍历电话号码
            // 1. 先取出个数
            let count = phones.count
            for i in 0..

你可能感兴趣的:(获取通讯录-RHAddressBook(swift))