Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable camera in MFMessageComposeViewController?

Tags:

ios

swift

I am developing an iOS application with a button to report an issue using SMS/iMessage. I am using MFMessageComposeViewController to present the message composition interface using the following code (Swift 3):

if(MFMessageComposeViewController.canSendText()){

        let controller = MFMessageComposeViewController()
        controller.messageComposeDelegate = self
        controller.body = "Example Message"
        controller.recipients = ["2345678901"]
        self.present(controller, animated: true, completion: nil)
    }

I have also implemented the MFMessageComposeViewControllerDelegate function to dismiss properly. A standard text message / iMessage sends successfully, but the user does not have the option to attach an image. The buttons for camera, iMessage Apps, etc. are there, but they are disabled and cannot be pressed. How can I enable these buttons (camera, specifically) to allow my users to attach images to messages composed with the app?

The Buttons in Question: The Buttons in Question

EDIT: Thanks Abdelahad for the suggestion. I've modified his response to allow multiple recipients and to include a message body. I also updated it to remove the deprecated addingPercentEscapes(using: ) method.

Here is a solution using a url to open the Messages app. NOTE: This takes users out of the app.

let recipients = "2345678901,3456789012" //Phone Numbers
let messageBody = "This is a test"
let sms: String = "sms://open?addresses=\(recipients)&body=\(messageBody)"
let smsEncoded = sms.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)
let url = URL(string: smsEncoded!)
UIApplication.shared.openURL(url!)

But still I would like a solution that does not take the user out of the app. Is this possible? Why would the MFMessageComposeViewController show the buttons without enabling them?

like image 228
Avery Pelle Avatar asked Feb 12 '26 15:02

Avery Pelle


1 Answers

Don't use MFMessageComposeViewController use UIApplication.shared.openURL(url!) but this will takes the user out of the app

var phoneToCall: String = "sms: +201016588557"
var phoneToCallEncoded = phoneToCall.addingPercentEscapes(using: String.Encoding.ascii)
var url = URL(string: phoneToCallEncoded)
UIApplication.shared.openURL(url!)
like image 143
Abdelahad Darwish Avatar answered Feb 14 '26 06:02

Abdelahad Darwish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!