Sending SMS in iOS with Swift

后端 未结 7 757
一向
一向 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:00

    Not sure if you really got the answer. I was in a similar hunt and came across this solution and got it to work.

    import UIKit
    import MessageUI
    
    class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
    
        @IBOutlet weak var phoneNumber: UITextField!
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        @IBAction func sendText(sender: UIButton) {
            if (MFMessageComposeViewController.canSendText()) {
                let controller = MFMessageComposeViewController()
                controller.body = "Message Body"
                controller.recipients = [phoneNumber.text]
                controller.messageComposeDelegate = self
                self.presentViewController(controller, animated: true, completion: nil)
            }
        }
    
        func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
            //... handle sms screen actions
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    
        override func viewWillDisappear(animated: Bool) {
            self.navigationController?.navigationBarHidden = false
        }
    }
    

提交回复
热议问题