gpt4 book ai didi

swift - 如何在 Swift 中使用 ABPeoplePickerNavigationController 选择联系人?

转载 作者:IT王子 更新时间:2023-10-29 05:15:15 25 4
gpt4 key购买 nike

我已将 ABPeoplePickerNavigationController 添加到我的第一个 View Controller 中。我希望当我选择一个联系人时显示要在其他 View Controller 中显示的信息,但我正在尝试使用我的代码并且当我单击联系人时这不会显示。这只会将联系人打开到 native 应用 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)
}

谢谢!

最佳答案

这是 iOS 9 的最新框架 - ContactsUI

  1. 导入联系人界面

  2. 符合 CNContactPickerDelegate (不需要方法)

  3. 创建一个联系人选择器对象并显示它:

    let peoplePicker = CNContactPickerViewController()

    peoplePicker.delegate = self

    self.presentViewController(peoplePicker, animated: true, completion: nil)
  4. 在 contactPickerDidCancel 委托(delegate)函数中关闭 CNContactPickerViewController

    func contactPickerDidCancel(picker: CNContactPickerViewController) {
    picker.dismissViewControllerAnimated(true, completion: nil)
    }
  5. 以下是我如何使用 didSelectContact 委托(delegate)函数访问联系人姓名、电话号码、电话号码标签和照片:

    func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
    //Dismiss the picker VC
    picker.dismissViewControllerAnimated(true, completion: nil)
    //See if the contact has multiple phone numbers
    if contact.phoneNumbers.count > 1 {
    //If so we need the user to select which phone number we want them to use
    let multiplePhoneNumbersAlert = UIAlertController(title: "Which one?", message: "This contact has multiple phone numbers, which one did you want use?", preferredStyle: UIAlertControllerStyle.Alert)
    //Loop through all the phone numbers that we got back
    for number in contact.phoneNumbers {
    //Each object in the phone numbers array has a value property that is a CNPhoneNumber object, Make sure we can get that
    if let actualNumber = number.value as? CNPhoneNumber {
    //Get the label for the phone number
    var phoneNumberLabel = number.label
    //Strip off all the extra crap that comes through in that label
    phoneNumberLabel = phoneNumberLabel.stringByReplacingOccurrencesOfString("_", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    phoneNumberLabel = phoneNumberLabel.stringByReplacingOccurrencesOfString("$", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    phoneNumberLabel = phoneNumberLabel.stringByReplacingOccurrencesOfString("!", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    phoneNumberLabel = phoneNumberLabel.stringByReplacingOccurrencesOfString("<", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    phoneNumberLabel = phoneNumberLabel.stringByReplacingOccurrencesOfString(">", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    //Create a title for the action for the UIAlertVC that we display to the user to pick phone numbers
    let actionTitle = phoneNumberLabel + " - " + actualNumber.stringValue
    //Create the alert action
    let numberAction = UIAlertAction(title: actionTitle, style: UIAlertActionStyle.Default, handler: { (theAction) -> Void in
    //Create an empty string for the contacts name
    var nameToSave = ""
    //See if we can get A frist name
    if contact.givenName == "" {
    //If Not check for a last name
    if contact.familyName == "" {
    //If no last name set name to Unknown Name
    nameToSave = "Unknown Name"
    }else{
    nameToSave = contact.familyName
    }
    }else{
    nameToSave = contact.givenName
    }

    // See if we can get image data
    if let imageData = contact.imageData {
    //If so create the image
    let userImage = UIImage(data: imageData)
    }
    //Do what you need to do with your new contact information here!
    //Get the string value of the phone number like this:
    actualNumber.stringValue
    })
    //Add the action to the AlertController
    multiplePhoneNumbersAlert.addAction(numberAction)
    }
    }
    //Add a cancel action
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (theAction) -> Void in
    //Cancel action completion
    })
    //Add the cancel action
    multiplePhoneNumbersAlert.addAction(cancelAction)
    //Present the ALert controller
    self.presentViewController(multiplePhoneNumbersAlert, animated: true, completion: nil)
    }else{
    //Make sure we have at least one phone number
    if contact.phoneNumbers.count > 0 {
    //If so get the CNPhoneNumber object from the first item in the array of phone numbers
    if let actualNumber = contact.phoneNumbers.first?.value as? CNPhoneNumber {
    //Get the label of the phone number
    var phoneNumberLabel = contact.phoneNumbers.first!.label
    //Strip out the stuff you don't need
    phoneNumberLabel = phoneNumberLabel.stringByReplacingOccurrencesOfString("_", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    phoneNumberLabel = phoneNumberLabel.stringByReplacingOccurrencesOfString("$", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    phoneNumberLabel = phoneNumberLabel.stringByReplacingOccurrencesOfString("!", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    phoneNumberLabel = phoneNumberLabel.stringByReplacingOccurrencesOfString("<", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    phoneNumberLabel = phoneNumberLabel.stringByReplacingOccurrencesOfString(">", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)

    //Create an empty string for the contacts name
    var nameToSave = ""
    //See if we can get A frist name
    if contact.givenName == "" {
    //If Not check for a last name
    if contact.familyName == "" {
    //If no last name set name to Unknown Name
    nameToSave = "Unknown Name"
    }else{
    nameToSave = contact.familyName
    }
    }else{
    nameToSave = contact.givenName
    }

    // See if we can get image data
    if let imageData = contact.imageData {
    //If so create the image
    let userImage = UIImage(data: imageData)
    }
    //Do what you need to do with your new contact information here!
    //Get the string value of the phone number like this:
    actualNumber.stringValue
    }
    }else{
    //If there are no phone numbers associated with the contact I call a custom funciton I wrote that lets me display an alert Controller to the user
    self.displayAlert("Missing info", message: "You have no phone numbers associated with this contact")
    }
    }
    }

关于swift - 如何在 Swift 中使用 ABPeoplePickerNavigationController 选择联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25218399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com