从iOS 9中的Contacts框架访问电话号码

时间:2016-03-11 00:03:40

标签: swift ios9

我无法打开CNLabeledValue来获取电话号码。这是我的代码(Swift),我目前得到的是<CNPhoneNumber: 0x12cefa000: countryCode=us, digits=4157119521>。我只需要访问digits值,因此我尝试在for number in result.phoneNumbers {}内打印。

lazy var contacts: [CNContact] = {
        let contactStore = CNContactStore()
        let keysToFetch = [
            CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),
            CNContactPhoneNumbersKey]

        // Get all the containers
        var allContainers: [CNContainer] = []
        do {
            allContainers = try contactStore.containersMatchingPredicate(nil)
        } catch {
            print("Error fetching containers")
        }

        var results: [CNContact] = []

        // Iterate all containers and append their contacts to our results array
        for container in allContainers {
            let fetchPredicate = CNContact.predicateForContactsInContainerWithIdentifier(container.identifier)

            do {
                let containerResults = try contactStore.unifiedContactsMatchingPredicate(fetchPredicate, keysToFetch: keysToFetch)
                for result in containerResults {

                    for number in result.phoneNumbers {

                        // WHAT SHOULD I PRINT HERE?????
                        print(number.value) 
                    }

                }
                results.appendContentsOf(containerResults)
            } catch {
                print("Error fetching results for container")
            }
        }

        return results
    }()

2 个答案:

答案 0 :(得分:0)

In [52]: print a [[1 2] [3 4] [5 6] [5 6] [1 2] [3 4] [5 6] [5 6] [1 2] [3 4] [5 6] [5 6] [5 6]] In [53]: print b [[1 2] [3 4] [5 6] [5 6] [1 2] [3 4] [5 6] [5 6] [5 6]] In [54]: timeit -n 10000 -r 5 np.concatenate((a, b)) 10000 loops, best of 5: 2.05 µs per loop In [55]: timeit -n 10000 -r 5 np.append(a, b, axis = 0) 10000 loops, best of 5: 2.41 µs per loop In [58]: np.concatenate((a, b)) Out[58]: array([[1, 2], [3, 4], [5, 6], [5, 6], [1, 2], [3, 4], [5, 6], [5, 6], [1, 2], [3, 4], [5, 6], [5, 6], [5, 6], [1, 2], [3, 4], [5, 6], [5, 6], [1, 2], [3, 4], [5, 6], [5, 6], [5, 6]]) In [59]: np.append(a, b, axis = 0) Out[59]: array([[1, 2], [3, 4], [5, 6], [5, 6], [1, 2], [3, 4], [5, 6], [5, 6], [1, 2], [3, 4], [5, 6], [5, 6], [5, 6], [1, 2], [3, 4], [5, 6], [5, 6], [1, 2], [3, 4], [5, 6], [5, 6], [5, 6]]) In [60]: np.append(a, b) Out[60]: array([1, 2, 3, 4, 5, 6, 5, 6, 1, 2, 3, 4, 5, 6, 5, 6, 1, 2, 3, 4, 5, 6, 5, 6, 5, 6, 1, 2, 3, 4, 5, 6, 5, 6, 1, 2, 3, 4, 5, 6, 5, 6, 5, 6]) 的属性stringValue是格式化的数字。一旦你不想要像“ - ”,“(”或“)”这样的符号,你只需要进一步的字符串装饰。

CNPhoneNumber

答案 1 :(得分:0)

if contact.isKeyAvailable(CNContactPhoneNumbersKey) {
                for phoneNumber in contact.phoneNumbers {
                    let label = CNLabeledValue.localizedStringForLabel(phoneNumber.label)

                    let phone = phoneNumber.value as! CNPhoneNumber
                        print(" \(label) \(phone.stringValue)")
                }
            }

这应该打印(即默认联系人Anna Haro):home 555-522-8243

然后你可以按照你想要的方式格式化“stringValue”