Sending SMS in iOS with Swift

后端 未结 7 765
一向
一向 2020-12-04 06:47

First of all, I\'m really surprised that this is not a duplicate, because there are TONS of stackoverflow questions that solve this in Objective-C, but I have yet to see a g

7条回答
  •  清歌不尽
    2020-12-04 07:10

    Swift 3, 4, 5

    @IBAction func sendSmsClick(_ sender: AnyObject) {
            guard MFMessageComposeViewController.canSendText() else {
                return
            }
    
            let messageVC = MFMessageComposeViewController()
    
            messageVC.body = "Enter a message";
            messageVC.recipients = ["Enter tel-nr"]
            messageVC.messageComposeDelegate = self;
    
            self.present(messageVC, animated: false, completion: nil)
        }
    
        func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
            switch (result.rawValue) {
                case MessageComposeResult.cancelled.rawValue:
                print("Message was cancelled")
                self.dismiss(animated: true, completion: nil)
            case MessageComposeResult.failed.rawValue:
                print("Message failed")
                self.dismiss(animated: true, completion: nil)
            case MessageComposeResult.sent.rawValue:
                print("Message was sent")
                self.dismiss(animated: true, completion: nil)
            default:
                break;
            }
        }
    

    UI will look like:

提交回复
热议问题