我已从地址簿中找到了联系人
现在我需要在同一个视图控制器中显示文本视图UI的名称。
如何从ABRecord中提取名称?
这是我的代码。
@IBAction func addContact(sender: AnyObject) {
var peoplePicker = ABPeoplePickerNavigationController()
peoplePicker.peoplePickerDelegate = self
self.presentViewController(peoplePicker, animated: true, completion: nil)
}
func peoplePickerNavigationControllerDidCancel(peoplePicker: ABPeoplePickerNavigationController!) {
dismissViewControllerAnimated(true, completion: nil)
}
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!) {
var name : String! = ABRecordCopyCompositeName(person)
}
这里函数ABRecordCopyCompositeName返回unmanaged我不能强制转换为String。它说不可转换。
我是iOS开发新手。请帮忙。
答案 0 :(得分:4)
使用takeUnretainedValue()和takeRetainedValue()获取CFString对象并将其强制转换为NSString
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!) {
let nameCFString : CFString = ABRecordCopyCompositeName(person).takeRetainedValue()
let name : NSString = nameCFString as NSString
}
答案 1 :(得分:-2)
您还可以查看KBContactsSelection,它允许您搜索和选择多个联系人,并且可以使用优雅Builder Pattern轻松自定义。