获取通讯录-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<UITouch>, 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..<count {
    
                // 取出标签
                let label = phones.labelAtIndex(i)
    
                // 取出电话号码的值
                let value = phones.valueAtIndex(i) as! String
                print(label, value)
    
            }
        }
    }
    
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容