Swift (iOS 8 SDK) Convert Unmanaged ABMultiValueRef to ABMultiValueRef
I found the solution:
func peoplePickerNavigationController(
peoplePicker: ABPeoplePickerNavigationController!,
didSelectPerson person: ABRecordRef!) {
/* Do we know which picker this is? */
if peoplePicker != personPicker{
return
}
/* Get all the phone numbers this user has */
let unmanagedPhones = ABRecordCopyValue(person, kABPersonPhoneProperty)
let phones: ABMultiValueRef =
Unmanaged.fromOpaque(unmanagedPhones.toOpaque()).takeUnretainedValue()
as NSObject as ABMultiValueRef
let countOfPhones = ABMultiValueGetCount(phones)
for index in 0..<countOfPhones{
let unmanagedPhone = ABMultiValueCopyValueAtIndex(phones, index)
let phone: String = Unmanaged.fromOpaque(
unmanagedPhone.toOpaque()).takeUnretainedValue() as NSObject as String
println(phone)
}
}
Swift Compile Error Bitcast requires both operands to be pointer or neither
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!,didSelectPerson person: ABRecordRef!) {
var unmanagedEmails = ABRecordCopyValue(person, 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)
}
More full answer can be found here.
How to fetch custom label phone numbers in address book swift
let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))
This will print the label of phone number. I believe this is what you looking, For more details Please visit here. Find the full code below.
EDIT
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)" )
}
}
}
}
Update : Swift - 4 From BadCode answer.
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)" )
}
}
OutPUT
Name :-_$!<Mobile>!$_ NO :-8592-841222
Name :-CUSTOMLABEL NO :-111
Name :-_$!<Home>!$_ NO :-45445
Middle one is my customised label,
Please note that default label always start with
_$!<
characters.
How do I use DLLImport with structs as parameters in C#?
The MSDN topic Passing Structures has a good introduction to passing structures to unmanaged code. You'll also want to look at Marshaling Data with Platform Invoke, and Marshaling Arrays of Types.
Bulk Email Validator
try running gethostbyname() on whatever is after the @ and if it doesnt return an IP, its not a valid DNS
Related Topics
Cannot Add Alamofire to Swift Project
Swiftui Coordinator Not Updating the Containing View's Property
Swift: Detecting an Unexpected Nil Value in a Non-Optional at Runtime: Casting as Optional Fails
Why Is an Object Not Being Decoded
Multiple Enum Types List All Cases
My Uiviewcontroller Is Not Filling the Entire Screen
How to Access Firebase Variable Outside Firebase Function
Fblpromises Framework Not Found
How to Set an Environment Object in Preview
Compare Three Values for Equality
Why Is Swift's Ternary Operator So Picky About Whitespace
Swift [1,2] Conforms to Anyobject But [Enum.A, Enum.B] Does Not
Label Disappear When Changing Font Size to 25 in Swift
Uibarbuttonitem Doesn't Work When Created as a Property, But Does When Created in a Function
How to Draw a Cosine or Sine Curve in Swift
What Is the Swift Equivalent of -[Nsobject Description]