gpt4 book ai didi

ios - 如何在地址簿中快速获取自定义标签电话号码

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

我正在尝试使用地址簿(swift)获取自定义标签电话号码。

我已经尝试使用 kABOtherLabel 属性,但没有得到想要的结果。

我想知道有什么方法可以获取自定义标签属性..?

在这里我分享一下我目前正在做的事情。

提前致谢。

//phone


var phones : ABMultiValueRef = ABRecordCopyValue(contactRef,kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValueRef

for(var numberIndex : CFIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++)
{

// Number in contact details of current index

let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)


let phoneNumber : NSString = phoneUnmaganed.takeUnretainedValue() as! NSString

// Label of Phone Number

let locLabel : CFStringRef = (ABMultiValueCopyLabelAtIndex(phones, numberIndex) != nil) ? ABMultiValueCopyLabelAtIndex(phones, numberIndex).takeUnretainedValue() as CFStringRef : ""

//check for home
if (String(locLabel) == String(kABHomeLabel))
{
contact.sUserTelHome = phoneNumber as String
contact.sUserTelHomeTrim = contact.sUserTelHome?.trimmedContactNumber()

}

//check for work
else if (String(locLabel) == String(kABWorkLabel))
{
contact.sUserTelWork = phoneNumber as String
contact.sUserTelWorkTrim = contact.sUserTelWork?.trimmedContactNumber()

}

//check for mobile
else if (String(locLabel) == String(kABPersonPhoneMobileLabel))
{
contact.sUserTelMobile = phoneNumber as String
contact.sUserTelMobileTrim = contact.sUserTelMobile?.trimmedContactNumber()
}

else if(String(locLabel) == String(kABOtherLabel)){



}
}

最佳答案

let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))

这将打印电话号码的标签。我相信这就是你要找的,更多详情请visit here .在下面找到完整的代码。

编辑

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 {
// now do something with the array of people

for record:ABRecordRef in people {
var phones : ABMultiValueRef = ABRecordCopyValue(record,kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValueRef

for(var numberIndex : CFIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++)
{
let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)


let phoneNumber : NSString = phoneUnmaganed.takeUnretainedValue() as! NSString

let locLabel : CFStringRef = (ABMultiValueCopyLabelAtIndex(phones, numberIndex) != nil) ? ABMultiValueCopyLabelAtIndex(phones, numberIndex).takeUnretainedValue() as CFStringRef : ""

var cfStr:CFTypeRef = locLabel
var nsTypeString = cfStr as! NSString
var swiftString:String = nsTypeString as String

let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))


println("Name :-\(swiftString) NO :-\(phoneNumber)" )
}
}


}
}

更新:Swift - 4 来自 BadCode 答案。

func getAllContactPhoneNumber() {
let phones: ABMultiValue = ABRecordCopyValue(person,
kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValue
for numberIndex in 0..<ABMultiValueGetCount(phones) {
let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)
guard let phoneNumber = phoneUnmaganed!.takeUnretainedValue() as? NSString else {
return
}
let locLabel: NSString = (ABMultiValueCopyLabelAtIndex(phones, numberIndex) != nil) ?
ABMultiValueCopyLabelAtIndex(phones, numberIndex).takeUnretainedValue() as NSString: ""
let cfStr: CFTypeRef = locLabel
guard let nsTypeString = cfStr as? NSString else {
return
}
let swiftString: String = nsTypeString as String
let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))
print("Name :-\(swiftString) NO :-\(phoneNumber)" )
}
}

输出

Name :-_$!<Mobile>!$_ NO :-8592-841222
Name :-CUSTOMLABEL NO :-111
Name :-_$!<Home>!$_ NO :-45445

中间一个是我的自定义标签,

Please note that default label always start with _$!< characters.

关于ios - 如何在地址簿中快速获取自定义标签电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29892687/

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