gpt4 book ai didi

ios - 在 Swift 中获取联系人姓名和电话号码

转载 作者:搜寻专家 更新时间:2023-10-31 22:03:08 26 4
gpt4 key购买 nike

我知道这个问题已经被问了很多,但我正在努力寻找一个完整的解决方案,而我找到的少数是在 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 = //??????
}

}
}
}

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

最佳答案

我找到了解决方案

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

关于ios - 在 Swift 中获取联系人姓名和电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31090143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com