-
-
Save congnt24/050b3e9705897575029e8d5654cca4d8 to your computer and use it in GitHub Desktop.
Send SMS Messages with Swift
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
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