MilkBottle
MilkBottle

Reputation: 4332

How to launch SMS Message app

I want to open SMS Message App to copy some text from friends. I am not creating SMS.

How to launch iphone SMS Message app using Swift code? I come across this code below for launching Mail app but not working.


[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];

I changed it to and the same not working

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]];

appreciate your help. TIA

Upvotes: 15

Views: 21761

Answers (4)

oskarko
oskarko

Reputation: 4178

Swift 5:

let sms = "sms:+1234567890&body=Hello Abc How are You I am ios developer."
let strURL = sms.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
UIApplication.shared.open(URL(string: strURL)!, options: [:], completionHandler: nil)

Upvotes: 26

Gaurav Chandarana
Gaurav Chandarana

Reputation: 754

The question is about how to launch the SMS Message App. But in case if someone wants to open the SMS composer without leaving the current app, here's a way to achive this.

Let's say the below function is triggered when the user taps on the send sms button

private func sendSMSButtonAction() {
    guard MFMessageComposeViewController.canSendText() else {
        print("Unable to send messages.")
        return
    }

    let controller = MFMessageComposeViewController()
    // controller.messageComposeDelegate = self // Confirm this if you want to check the result when the user dismisses the controller.
    controller.recipients = ["+12345678901"]
    controller.body = "Some text"
    present(controller, animated: true)
}

Please make sure you've imported the MessageUI in your controller.

Upvotes: 0

Reming Hsu
Reming Hsu

Reputation: 2225

// Swift 3
UIApplication.sharedApplication().openURL(NSURL(string: "sms:")!)

// Swift 4
UIApplication.shared.open(URL(string: "sms:")!, options: [:], completionHandler: nil)

Upvotes: 21

Josip B.
Josip B.

Reputation: 2464

I'm not sure why you want to explicitly use SMS app and I'm not sure if it's possible. On the other hand iOS by default offers MFMessageComposeViewController for sending SMS from iOS app.

Check this Swift example for more details.

Edit: I've found this wiki page which may contain answer for your question. Be aware I haven't tested it.

let number = "sms:+12345678901"
UIApplication.sharedApplication().openURL(NSURL(string: number)!)

Upvotes: 5

Related Questions