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

时间:2015-04-27 10:04:08

标签: ios swift addressbook abaddressbook

我正在尝试使用地址簿(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)){



            }
}

3 个答案:

答案 0 :(得分:4)

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

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

  

请注意,默认标签始终以_$!<个字符开头。

答案 1 :(得分: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)" )

                    }
                }


            }
        }

答案 2 :(得分:0)

适用于Swift 4(更新Aju&#39的代码)。也为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)" )
    }
}