获取Swift中的联系人姓名和电话号码

时间:2015-06-27 15:06:31

标签: ios swift

我知道这已被问了很多,但我很难找到一个完整的解决方案,而我发现的很少是在Objective C中

我已成功实现这一目标

public static func refreshContacts(){
    let status = ABAddressBookGetAuthorizationStatus()
    if status == .Denied || status == .Restricted {
        // user previously denied, to tell them to fix that in settings
        return
    }

    // open it

    var error: Unmanaged<CFError>?
    let addressBook: ABAddressBook? = ABAddressBookCreateWithOptions(nil, &error)?.takeRetainedValue()
    if addressBook == nil {
        println(error?.takeRetainedValue())
        return
    }

    // request permission to use it

    ABAddressBookRequestAccessWithCompletion(addressBook) {
        granted, error in

        if !granted {
            // warn the user that because they just denied permission, this functionality won't work
            // also let them know that they have to fix this in settings
            return
        }

        if let people = ABAddressBookCopyArrayOfAllPeople(addressBook)?.takeRetainedValue() as? NSArray {
            for person in people{
                var name = //??????
                var phoneumber = //??????
            }

        }
    }
}

如果您阅读评论,您可以看到有一点我不太确定该怎么做 我怎样才能得到姓名和电话号码?

1 个答案:

答案 0 :(得分:7)

我找到了解决方案

public static func refreshContacts(){
    let status = ABAddressBookGetAuthorizationStatus()
    if status == .Denied || status == .Restricted {
        // user previously denied, to tell them to fix that in settings
        return
    }

    // open it

    var error: Unmanaged<CFError>?
    let addressBook: ABAddressBook? = ABAddressBookCreateWithOptions(nil, &error)?.takeRetainedValue()
    if addressBook == nil {
        println(error?.takeRetainedValue())
        return
    }

    // request permission to use it

    ABAddressBookRequestAccessWithCompletion(addressBook) {
        granted, error in

        if !granted {
            // warn the user that because they just denied permission, this functionality won't work
            // also let them know that they have to fix this in settings
            return
        }

        if let people = ABAddressBookCopyArrayOfAllPeople(addressBook)?.takeRetainedValue() as? NSArray {
            for person in people{
                if let name = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeRetainedValue() as? String {
                    println(name)//persons name
                }
                let numbers:ABMultiValue = ABRecordCopyValue(
                    person, kABPersonPhoneProperty).takeRetainedValue()
                for ix in 0 ..< ABMultiValueGetCount(numbers) {
                    let label = ABMultiValueCopyLabelAtIndex(numbers,ix).takeRetainedValue() as String
                    let value = ABMultiValueCopyValueAtIndex(numbers,ix).takeRetainedValue() as! String
                    println("Phonenumber \(label) is \(value))
                }
            }

        }
    }
}

改编自https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch18p713addressBook/ch31p973addressBook/ViewController.swift

相关问题