如何在Swift中获取地址簿中的自定义标签电话号码

4

我正在尝试使用通讯录(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)){



            }
}

尝试过 kABPersonNoteProperty 吗? - Ajumal
我认为kABPersonNote属性只负责读取备注,不包括电话号码。 - Anil Kukadeja
3个回答

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

这将打印电话号码的标签。我相信这是您所寻找的,如需更多详细信息,请访问此处。以下是完整代码。 编辑
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

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

请注意,默认标签总是以_$!<字符开头。


它只是打印属性的名称。我怎么知道来自通讯录的号码是来自自定义标签的呢?非常感谢您的帮助! - Anil Kukadeja
地址簿是否有限制,即联系人至少应具有一个主要标签号码?我遇到了一些问题,例如如果我仅使用自定义标签存储联系人“Aju”,则无法获取该联系人。但是,如果我编辑该联系人并添加任何一个主要标签,则它将出现在我的列表中。 - Anil Kukadeja
1
@anilkukdeja,ABAddressBookCopyLocalizedLabel返回CFTypeRef,您需要将其转换为字符串。请查看输出,更新的代码打印正确的值。 - Ajumal
@Aju,请将代码更新为Swift 3,并保持相同的输出。 - Rajamohan S

1

适用于Swift 3的代码(对Aju代码的更新):

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 {
            print(error?.takeRetainedValue() as Any)
            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: Array<ABRecord> = ABAddressBookCopyArrayOfAllPeople(self.addressBookRef)?.takeRetainedValue() as Array<ABRecord>? {
                // now do something with the array of people

                for record:ABRecord in people {
                let phones : ABMultiValue = ABRecordCopyValue(record,kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValue

                for numberIndex:CFIndex in 0 ... ABMultiValueGetCount(phones)-1
                {
                    let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)
                    let phoneNumber : String = phoneUnmaganed!.takeUnretainedValue() as! String

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

                    //type of phone number
                    let cfStr:CFTypeRef = locLabel
                    let nsTypeString = cfStr as! String
                    let swiftString:String = nsTypeString as String

                    let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))
                    print("Name \(swiftString) NO \(phoneNumber)" )

                    }
                }


            }
        }

0

适用于Swift 4(Aju代码的更新)。还进行了SwiftLint的编辑。

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)" )
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接