Last active
April 4, 2017 15:11
-
-
Save DonMag/220cb3754e382c064799e19e6037b28e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| -------------------- | |
| func makeTheCall() { | |
| // if we don't have a Contact, get a Contact | |
| // EXIT THIS FUNCTION | |
| // if we get here, we have Contact | |
| // if we don't have a Phone number, get a number | |
| // EXIT THIS FUNCTION | |
| // if we get here, we have a Contact and a Phone number | |
| // 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 { | |
| getAContact() | |
| return // this EXITS this function | |
| } | |
| // if we get to here, we have a Contact | |
| if self.numberToCall == nil { | |
| getAPhone() | |
| return // this EXITS this function | |
| } | |
| // 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 getAContact() { | |
| // find a match | |
| let matches = findPeople(word1, word2) | |
| if matches.count == 0 { | |
| // no matching contacts... | |
| // either present a full Contact selection list or | |
| // show an error message and abort trying to make a call | |
| return // this EXITS this function | |
| } | |
| if matches.count > 1 { | |
| // found multiple matches, so present a list | |
| pickContact(matches) | |
| return // this EXITS this function | |
| } | |
| if matches.count == 1 { | |
| // found ONE matching contact | |
| self.contactToCall = matches[0] | |
| // go back to Making the Call | |
| makeTheCall() | |
| } | |
| } | |
| func pickContact(matches) { | |
| // present a list of Names from the matches[] array | |
| // UIAlertAction() for a selection is: | |
| { | |
| self.contactToCall = selection | |
| // go back to making the call | |
| makeTheCall() | |
| } | |
| // make sure you also handle Cancel | |
| self.presentViewController(selectContactAlert, animated: true, completion: nil) | |
| } | |
| func getAPhone() { | |
| let numbers = phonesForContact(self.contactToCall) | |
| if numbers.count == 0 { | |
| // Contact has no phone... show message and abort | |
| return // this EXITS this function | |
| } | |
| if numbers.count > 1 { | |
| // Contact has multiple phones, so present a list | |
| pickPhone(numbers) | |
| return // this EXITS this function | |
| } | |
| if numbers.count == 1 { | |
| // Contact has ONE phone, so go on to actually make the call | |
| self.numberToCall = numbers[0] | |
| // go back to Making the Call | |
| makeTheCall() | |
| } | |
| } | |
| func pickPhone(numbers) { | |
| // present a list of Phone numbers from the numbers[] array | |
| // UIAlertAction() for a selection is: | |
| { | |
| self.numberToCall = selection | |
| // go back to making the call | |
| 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