ios – 如何在地址簿swift中获取自定义标签电话号码

ios – 如何在地址簿swift中获取自定义标签电话号码,第1张

概述我正在尝试使用地址簿( swift)获取自定义标签电话号码. 我尝试过kABOtherLabel属性,但我没有得到理想的结果. 我想知道有没有办法获取自定义标签属性..? 在这里,我分享我目前正在做的事情. 提前致谢. //phonevar phones : ABMultiValueRef = ABRecordCopyValue(contactRef,kABPersonPhoneProper 我正在尝试使用地址簿( swift)获取自定义标签电话号码.

我尝试过kABOtherLabel属性,但我没有得到理想的结果.

我想知道有没有办法获取自定义标签属性..?

在这里,我分享我目前正在做的事情.

提前致谢.

//phonevar 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).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 ABMultiValuefor 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-841222name :-CUSTOMLABEL NO :-111name :-_$!<Home>!$_ NO :-45445

中间一个是我的定制标签,

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

总结

以上是内存溢出为你收集整理的ios – 如何在地址簿swift中获取自定义标签电话号码全部内容,希望文章能够帮你解决ios – 如何在地址簿swift中获取自定义标签电话号码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1061387.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存