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

Ham*_*lik 3 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 = //??????
            }

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Ham*_*lik 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))
                }
            }

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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