Sending Sms in iOS with Swift

Sending SMS in iOS with Swift

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
}
}

Send a text message directly from an iOS App

The answer us no without an API like Twilio, ZipWhip or Plivo. You can, however, create a message when the user taps a link via the "sms:" URL scheme. See https://developer.apple.com/library/content/featuredarticles/iPhoneURLScheme_Reference/SMSLinks/SMSLinks.html

Send SMS in iOS

I finally did it, I mixed up two solutions,
First of all,i changed the function of sendMessage and renamed it to sendText, in this way :

    @IBAction func sendText(sender: AnyObject) {
if (MFMessageComposeViewController.canSendText()) {
let controller = MFMessageComposeViewController()
controller.body = "Text Body"
controller.recipients = ["xxxxxxxxxxx"]
controller.messageComposeDelegate = self
self.presentViewController(controller, animated: true, completion: nil)
}
}

And the protocol of MessageComposeViewController is :

func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
switch (result.rawValue) {
case MessageComposeResultCancelled.rawValue:
print("Message was cancelled")
self.dismissViewControllerAnimated(true, completion: nil)
case MessageComposeResultFailed.rawValue:
print("Message failed")
self.dismissViewControllerAnimated(true, completion: nil)
case MessageComposeResultSent.rawValue:
print("Message was sent")
self.dismissViewControllerAnimated(true, completion: nil)
default:
break;
}
}

And then click on the button in storyboards, and press control key and drag the button to ViewController and select the sendText function, now it works correctly.

Thx to my Friend @Kabiroberai for help and give some time.

How to send a message in iOS

No, that is not available to directly send the messages with MFMessageComposeViewController, it will follow the flow. It will present you with the ComposerView where you have to manually press the send button to send the SMS.

To use without composerView, you should try some third party API's which can be helpful.



Related Topics



Leave a reply



Submit