Skip to content

Instantly share code, notes, and snippets.

@DonMag
Created April 4, 2017 14:37
Show Gist options
  • Select an option

  • Save DonMag/4632ef1c31a7516cf23d3c22284e4410 to your computer and use it in GitHub Desktop.

Select an option

Save DonMag/4632ef1c31a7516cf23d3c22284e4410 to your computer and use it in GitHub Desktop.
You've parsed the dictation and determined the user wants to call a contact...
1. Find the Contact
2. Get the Phone Number
3. Actually make the call
--------------------
@IBAction func buttonProcess(sender: AnyObject) {
...
case "Call":
resultMessage.text = "Switching to Phone for your call"
// I'm assuming by this point you have determined that
// "actionType" is "Call" and
// you have word1 and word2
makeTheCall()
break
...
}
var contactToCall: CNContact?
var numberToCall: String?
func makeTheCall() {
if self.contactToCall == nil {
// find a match
let matches = findPeople(word1, word2)
if matches.count == 0 {
// no matching contacts... either present a full Contact selection list or abort
return
}
if matches.count > 1 {
// found multiple matches, so present a list
pickContact(matches)
return
}
if matches.count == 1 {
// found ONE matching contact, so go on to get the phone number
self.contactToCall = matches[0]
}
}
// if we get to here, we have a Contact
if self.numberToCall == nil {
let numbers = phonesForContact(self.contactToCall)
if numbers.count == 0 {
// Contact has no phone... show message and abort
return
}
if numbers.count > 1 {
// Contact has multiple phones, so present a list
pickPhone(numbers)
return
}
if numbers.count == 1 {
// Contact has ONE phone, so go on to actually make the call
self.numberToCall = numbers[0]
}
}
// if we get to here, we found (or user selected) a Contact, and found (or user selected) a Phone, so
// make the call
if let url = NSURL(string: "tel://\(self.numberToCall)") {
UIApplication.sharedApplication().openURL(url)
}
} // end of makeTheCall()
func pickContact(matches) {
// present a list of Names from the matches[] array
// UIAlertAction() for a selection is:
self.contactToCall = selection
self.makeTheCall()
// make sure you also handle Cancel
self.presentViewController(selectContactAlert, animated: true, completion: nil)
}
func pickPhone(numbers) {
// present a list of Phone numbers from the numbers[] array
// UIAlertAction() for a selection is:
self.numberToCall = selection
self.makeTheCall()
// make sure you also handle Cancel
self.presentViewController(selectPhoneAlert, animated: true, completion: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment