Skip to content

Instantly share code, notes, and snippets.

@soggybag
Last active August 28, 2017 15:13
Show Gist options
  • Save soggybag/1c863ab275e1c0e09936 to your computer and use it in GitHub Desktop.
Save soggybag/1c863ab275e1c0e09936 to your computer and use it in GitHub Desktop.
Send SMS Messages with Swift
import UIKit
import MessageUI // Import MessageUI
// Add the delegate protocol
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
// Send a message
func sendMessage() {
let messageVC = MFMessageComposeViewController()
messageVC.body = "Message String"
messageVC.recipients = [] // Optionally add some tel numbers
messageVC.messageComposeDelegate = self
presentViewController(messageVC, animated: true, completion: nil)
}
// Conform to the protocol
// MARK: - Message Delegate method
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
switch result.rawValue {
case MessageComposeResultCancelled.rawValue :
print("message canceled")
case MessageComposeResultFailed.rawValue :
print("message failed")
case MessageComposeResultSent.rawValue :
print("message sent")
default:
break
}
controller.dismissViewControllerAnimated(true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment