Make Call, Send Email, SMS using Swift

Make Call :

Add following code to button of any function which you are using to make call

  @IBAction func btnMakeCall(_ sender: Any) {
   
        guard let number = URL(string: "tel://" + "8********3") 
        else { return }
        UIApplication.shared.open(number)
    }

Send Email :

Step 1: Import

import MessageUI

Step 2 : Declare delegate

class ViewController: UIViewController, MFMailComposeViewControllerDelegate

Step 3 : Add following code with delegate method

  @IBAction func btnSendMail(_ sender: Any) {
        
        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setToRecipients(["abc.xyz@gmail.com"])
            mail.setSubject("Subject")
            mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
            mail.setCcRecipients(["xyz.abc@gmail.com"])
            
            present(mail, animated: true)
        } else {
            // show failure alert
        }
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        //... handle email screen actions
        controller.dismiss(animated: true)
    }

Send SMS :

Step 1: Import

import MessageUI

Step 2 : Declare delegate

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate

Step 3 : Add following code with delegate method

    @IBAction func btnSendSMS(_ sender: Any) {
        
        if (MFMessageComposeViewController.canSendText()) {
            let controller = MFMessageComposeViewController()
            controller.body = "Message Body"
            controller.recipients = ["8********2"]
            controller.messageComposeDelegate = self
            self.present(controller, animated: true, completion: nil)
        }
        
    }
    
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        //... handle sms screen actions
        self.dismiss(animated: true, completion: nil)
    }

240_F_77959340_hWLiOY93juohUoXqjKzqPB79552nw8XU

Hope you find this blog useful. Please feel free to contact with me in case you have any query, suggestions.  You can comment, like and follow posts.

You can request any topic related to Swift and iOS development.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s