Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending SMS in iOS with Swift

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 good answer that used Swift.

What I'm looking for is a code snippet in Swift that sends an arbitrary string as a the body of a text message to given phone number. Essentially, I'd like something like this from Apple's official documentation, but in Swift instead of Objective-C.

I imagine this isn't too difficult, as it can be done in just a couple of lines of code in Android.

EDIT: What I'm looking for is 5-20 lines of Swift code, I do not agree that this is too broad. In Java (for Android), the solution looks like this:

package com.company.appname; import android.app.Activity; import android.telephony.SmsManager; public class MainActivity extends Activity {     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         public static final mPhoneNumber = "1111111111";         public static final mMessage = "hello phone";         SmsManager.getDefault().sendTextMessage(mPhoneNumber, null, mMessage, null, null);      } } 

Now this is the android solution, and it's only 11 lines. Java tends to be much more verbose than Swift, so I doubt what I'm asking is "too broad", it is more likely that I don't know how to use the Objective-C MessageComposer object, because the documentation that I linked to above is unclear with regard to usage in Swift.

like image 691
johncorser Avatar asked Oct 13 '14 22:10

johncorser


People also ask

Can iOS apps send SMS?

You still cannot send fully automated SMS from the iPhone itself, it requires some user interaction. But this at least allows you to populate everything, and avoids closing the application.

Can an app send SMS?

A lot of people look for free texting apps so they can text on non-connected devices like tablets. Pulse SMS, Android Messages, AirDroid, Pushbullet, etc are all excellent options for this.


1 Answers

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     } } 
like image 185
sivag1 Avatar answered Sep 18 '22 00:09

sivag1