如何在Swift中与ABPeoplePickerNavigationController选择联系人?

我已将ABPeoplePickerNavigationController添加到我的第一个视图控制器中.我想要当我选择一个联系人显示信息显示在其他视图控制器,但我正在尝试使用我的代码,这不会显示从来没有当我点击联系人.这只能将联系人打开为本机应用程序ABPeoplePickerNavigationController.
var people = ABPeoplePickerNavigationController()
var addressBook: ABAddressBookRef?

func extractABAddressBookRef(abRef: Unmanaged<ABAddressBookRef>!) -> ABAddressBookRef? {
    if let ab = abRef {
        self.view.addSubview(people.view)
        return Unmanaged<NSObject>.fromOpaque(ab.toOpaque()).takeUnretainedValue()
    }
    return nil
}

我试过这个功能

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!,didSelectPerson person: ABRecordRef!) {

    var unmanagedEmails = ABRecordCopyValue(people,kABPersonEmailProperty)
    let emailObj: ABMultiValueRef = Unmanaged.fromOpaque(unmanagedEmails.toOpaque()).takeUnretainedValue() as NSObject as ABMultiValueRef

    var index = 0 as CFIndex

    var unmanagedEmail = ABMultiValueCopyValueAtIndex(emailObj,index)
    var emailAddress:String = Unmanaged.fromOpaque(unmanagedEmail.toOpaque()).takeUnretainedValue() as NSObject as String

    println(emailAddress)      
}

谢谢!

@H_403_12@ 几个想法:

>你设置peoplePickerDelegate属性的人选择器控制器?如果你不这样做,就不会在课堂上尝试这些方法.从而:

people.peoplePickerDelegate = self
presentViewController(people,animated: true,completion: nil)

>当您调用ABRecordCopyValue时,您的示例方法引用了人.这是你的选择器控制器.我假设你的意思是引用人,ABRecordRef!作为参数传递.

您可能还想确保您在尝试访问电子邮件之前确实有一个电子邮件地址.您可以使用ABMultiValueGetCount.

我也认为你也可以从Opaque / toOpaque舞蹈中消除.

这产生:

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController,didSelectPerson person: ABRecord) {
    let emails: ABMultiValueRef = ABRecordCopyValue(person,kABPersonEmailProperty).takeRetainedValue()
    if ABMultiValueGetCount(emails) > 0 {
        let index = 0 as CFIndex
        let emailAddress = ABMultiValueCopyValueAtIndex(emails,index).takeRetainedValue() as! String

        print(emailAddress)
    } else {
        print("No email address")
    }
}

>如果您还需要支持iOS 7,请使用:

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController,shouldContinueAfterSelectingPerson person: ABRecord,property: ABPropertyID,identifier: ABMultiValueIdentifier) -> Bool {
    let multiValue: ABMultiValueRef = ABRecordCopyValue(person,property).takeRetainedValue()
    let index = ABMultiValueGetIndexForIdentifier(multiValue,identifier)
    let email = ABMultiValueCopyValueAtIndex(multiValue,index).takeRetainedValue() as! String

    print("email = \(email)")

    peoplePicker.dismissViewControllerAnimated(true,completion: nil)

    return false
}

>但是,您可能不是假设用户只想要第一个电子邮件地址,而是让他们点击并选择联系人可能的多个电子邮件地址之一.所以,首先,你可能想要消除一些“噪音”,告诉选择器你只想看到电子邮件地址:

people.peoplePickerDelegate = self
people.displayedProperties = [NSNumber(int: kABPersonEmailProperty)]
presentViewController(people,completion: nil)

然后,删除我们一直在讨论的先前的方法,而不是实现:

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!,didSelectPerson person: ABRecordRef!,identifier: ABMultiValueIdentifier) {
    let multiValue: ABMultiValueRef = ABRecordCopyValue(person,index).takeRetainedValue() as String

    println("email = \(email)")
}

而且,为了支持iOS 7,您还可以添加

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController,completion: nil)

    return false
}

>顺便说一下,iOS 8提供了一个功能来控制联系人是否启用.由于您支持iOS 7和8,您需要有条件地使用,例如:

if people.respondsToSelector(Selector("predicateForEnablingPerson")) {
    people.predicateForEnablingPerson = NSPredicate(format: "emailAddresses.@count > 0")
}

这给用户可视化指示是否还有个人的电子邮件地址,并阻止他们选择没有电子邮件地址的条目.

显然,如果使用iOS 9和更高版本,您应该退出所有这些,并使用ContactsUI框架,这进一步简化了代码.

相关文章

Swift 正式开源!Swift 团队很高兴宣布 Swift 开始开源新篇章。自从苹果发布 Swfit 编程语言,就成为了...
快,快,快!动动您的小手,分享给更多朋友! 苹果去年推出了全新的编程语言Swift,试图让iOS开发更简单...
开发者(KaiFaX) 面向开发者、程序员的专业平台! 和今年年初承诺的一样,苹果贴出了Swift语言的源码,...
本文由@Chun发表于Chun Tips :http://chun.tips/blog/2014/12/11/shi-yong-swift-gou-jian-zi-ding-yi...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...
本文由CocoaChina译者leon(社区ID)翻译 原文:THE RIGHT WAY TO WRITE A SINGLETON 在之前的帖子里聊过...