2

我正在尝试实现该func personViewController(personViewController: ABPersonViewController!, shouldPerformDefaultActionForPerson person: ABRecord!, property property: ABPropertyID, identifier valueIdentifier: ABMultiValueIdentifier) -> Bool函数,该函数是ABPersonViewControllerDelegate协议的一部分,每当用户单击 中的项目时都会调用该函数,ABPersonViewController这样用户选择的任何信息都将被复制到[String : String]字典中,从而属性名称将成为键对于属性值:[..."kABPersonFirstNameProperty" : "Alexander"...],比如说。

我还想避免开关或一长串条件测试属性是这个还是那个;我宁愿尽可能一般地处理它——我只尝试两种不同的情况:属性是单值还是多值。如果它是多值,我想复制所有可用的信息。

例如,如果用户点击地址,结果可能是这样的:[..."kABPersonAddressStreetKey" : "1 Infinite Loop", "kABPersonAddressCityKey" : "Cupertino" , "kABPersonAddressStateKey", "California (or CA?)"...].

这就是我所拥有的,经过几个小时的搜索 Apple Developer Library 和相关的 SO 问题(可悲,我知道):

func personViewController(personViewController: ABPersonViewController!,
    shouldPerformDefaultActionForPerson person: ABRecord!,
    property property: ABPropertyID,
    identifier valueIdentifier: ABMultiValueIdentifier) -> Bool {

    s["kABPersonFirstNameProperty"] = ABRecordCopyValue(person, kABPersonFirstNameProperty) as! String //the name can't actually be selected by the user, but I want to capture it anyway
    s["kABPersonLastNameProperty"] = ABRecordCopyValue(person, kABPersonLastNameProperty) as! String

    if valueIdentifier == 0 { //property is a single property, not a multivalue property

        let record = ABRecordCopyValue(person, property)
        s[property as! String!] = record as! String

    } else { //property is an ABMultiValue

        let multiRecord = ABRecordCopyValue(person, property) as! ABMultiValueRef
        s[property as! String] = ABMultiValueGetIndexForIdentifier(multiRecord, valueIdentifier)

    }

    return false

}

我可以说有很多片段丢失了——甚至可以把它浓缩成字典吗?

在此先感谢(以及 100 声望提供完整、正确答案的人)。

4

2 回答 2

2

使用 vadian 的答案作为起点,我编译了以下内容,它将收集并复制到字典中几乎所有用户可以选择的信息(以及一些用户无法点击的信息——名字、姓氏、组织-自动地)。

这是一项正在进行的工作,随着我继续添加对更多属性的支持,我将对其进行更新,但目前它适用于名字、姓氏、电子邮件、电话号码、地址和一些社交资料。

在某些情况下,我不得不使用间接方法:例如,将标签没有指定键的电话号码存储为kAB_Labels,并且它还没有使用电子邮件地址的标签。

对于stype Dictionary<String, AnyObject>,这里是:

func personViewController(personViewController: ABPersonViewController!, shouldPerformDefaultActionForPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) -> Bool {

    addPropertyForKey("kABPersonFirstNameProperty", person : person, property: kABPersonFirstNameProperty)
    addPropertyForKey("kABPersonLastNameProperty", person : person, property: kABPersonLastNameProperty)
    addPropertyForKey("kABPersonOrganizationProperty", person: person, property: kABPersonOrganizationProperty)

    if (property == kABPersonAddressProperty) {
        let multiRecord : ABMultiValueRef = ABRecordCopyValue(person, property).takeUnretainedValue()
        let index = ABMultiValueGetIndexForIdentifier(multiRecord, identifier)
        let addressRecord :AnyObject = ABMultiValueCopyValueAtIndex(multiRecord, index).takeUnretainedValue()
        addValuesFromRecord(addressRecord, forKeys:["Street", "City", "State", "ZIP", "Country", "CountryCode"])

    }

    if (property == kABPersonEmailProperty) {

        let multiRecord: ABMultiValueRef = ABRecordCopyValue(person, property).takeUnretainedValue()
        let index = ABMultiValueGetIndexForIdentifier(multiRecord, identifier)
        let email = ABMultiValueCopyValueAtIndex(multiRecord, index).takeUnretainedValue() as! String

        if (s["kABPersonEmailProperty(1)"] == nil) {

            s["kABPersonEmailProperty(1)"] = email

        } else {

            s["kABPersonEmailProperty(2)"] = email

        }

    }

    if (property == kABPersonSocialProfileProperty) {

        let multiRecord: ABMultiValueRef = ABRecordCopyValue(person, property).takeUnretainedValue()
        let index = ABMultiValueGetIndexForIdentifier(multiRecord, identifier)
        let profile = ABMultiValueCopyValueAtIndex(multiRecord, index).takeUnretainedValue()
        let profileType = profile["service"] as! String
        let profileName = profile["username"] as! String

        switch profileType {
            case "facebook" : s["kABPersonSocialProfileServiceFacebook"] = profileName
            case  "twitter" : s["kABPersonSocialProfileServiceTwitter"] = profileName
            case "sinaweibo": s["kABPersonSocialProfileServiceSinaWeibo"] = profileName
            default: break
        }



    }

    if (property == kABPersonPhoneProperty) {

        let multiRecord : ABMultiValueRef = ABRecordCopyValue(person, property).takeUnretainedValue()
        let index = ABMultiValueGetIndexForIdentifier(multiRecord, identifier)
        let number = ABMultiValueCopyValueAtIndex(multiRecord, index).takeUnretainedValue() as! String

        let locLabel: CFStringRef = (ABMultiValueCopyLabelAtIndex(multiRecord, index) != nil) ? ABMultiValueCopyLabelAtIndex(multiRecord, index).takeUnretainedValue() as CFStringRef : ""
        let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))

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

        var b = a

        if (a.rangeOfString("_$!<") != nil) {

            b = a.substringFromIndex(a.rangeOfString("_$!<")!.endIndex)

            b = b.substringToIndex(b.rangeOfString(">!$_")!.startIndex)

        }

        switch b {

        case "Mobile" : s["kABPersonPhoneMobileLabel"] = number
        case "iPhone" : s["kABPersonPhoneIPhoneLabel"] = number
        case "Main" : s["kABPersonPhoneMainLabel"] = number
        case "Home" : s["kABHomeLabel"] = number
        case "Work" : s["kABWorkLabel"] = number
        case "Other" : s["kABOtherLabel"] = number
        default: break

        }

    }

    println(s)

    return false
}

拜托,任何人都应该随意复制它的任何/所有部分,这会有所帮助。

于 2015-08-06T21:37:42.383 回答
0

恐怕没有通用的方法可以从 AddressBook 中检索信息,因为有太多不同的集合和属性类型。

这是用户点击地址字段时获取first name,last name和信息的示例。address

假设字典s被声明为

var s = Dictionary<String, AnyObject>()

 func personViewController(personViewController: ABPersonViewController!, shouldPerformDefaultActionForPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) -> Bool {

    addPropertyForKey("FirstName", person : person, property: kABPersonFirstNameProperty)
    addPropertyForKey("LastName", person : person, property: kABPersonLastNameProperty)

    if (property == kABPersonAddressProperty) {
      let multiRecord : ABMultiValueRef = ABRecordCopyValue(person, property).takeUnretainedValue()
      let index = ABMultiValueGetIndexForIdentifier(multiRecord, identifier)
      let addressRecord :AnyObject = ABMultiValueCopyValueAtIndex(multiRecord, index).takeUnretainedValue()
      addValuesFromRecord(addressRecord, forKeys:["Street", "City", "State", "ZIP", "Country", "CountryCode"])

      println(s)
    }

    return false
  }

  func addPropertyForKey(key : String, person: ABRecord, property : ABPropertyID)
  {
    let value : AnyObject = ABRecordCopyValue(person, property).takeUnretainedValue()
    s[key] = value
  }

  func addValuesFromRecord(record : AnyObject,  forKeys keys : [String])
  {
    for key in keys {
      if let value : AnyObject = record[key] {
        s[key] = value
      }
    }
  }

为了尽可能通用,所有值都声明为AnyObject

于 2015-08-06T11:16:18.500 回答