iOS Question How to add attachment to SMS message?

JohnC

Expert
Licensed User
Longtime User
This Tutorial shows how to send emails or SMS in iOS:


And it includes an example of how to send an attachment when sending "EMAILS".

But, how can I send an attachment when sending an SMS?
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
Or is there a way to sent an "Intent" of sort to the iMessage app that will allow me to specify the text and an attachment, so the end result will be that my app will cause the iMessage app to pop-up with my text message pre-filled in and the attachment all ready to be sent simply by the user clicking "Send" in the iMessage app.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Aehm.... A SMS is just a small piece of Text. There is no such thing as an Attachment in an SMS.

Or did i interpret the question wrong?
Note that i do not own a iOS Device as of now so i do not know iMessage.

 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Yes, it's true that "technically" SMS messages don't have attachments. But, since pretty much every SMS app on both android and iOS also support MMS messages (that have attachments) and these same apps can receive both SMS and MMS, the two terms are pretty synonymous with one another as opposed to Emails. I just mentioned the term SMS because that is the term the example mentioned and I just wanted to make sure I differentiated my question from the email example of the code.

So, from now on when you see me mention SMS, I could also be referring to MMS;)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private MessageComposer As MessageComposer
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
    MessageComposer.Initialize("MessageComposer")
    If MessageComposer.CanSendText Then
        MessageComposer.Body = "test"
        Log(AddAttachment(MessageComposer, "abc".GetBytes("utf8"), "plain.text", "1.txt"))
        MessageComposer.Show(Page1)
    End If
End Sub

Private Sub AddAttachment (mc As MessageComposer, data() As Byte, UTI As String, FileName As String) As Boolean
    Dim ControllerClass As NativeObject
    ControllerClass.Initialize("MFMessageComposeViewController")
    If ControllerClass.RunMethod("canSendAttachments", Null).AsBoolean Then
        If ControllerClass.RunMethod("isSupportedAttachmentUTI:", Array(UTI)).AsBoolean Then
            Dim no As NativeObject = mc
            Return no.RunMethod("addAttachmentData:typeIdentifier:filename:", _
                Array(no.ArrayToNSData(data), UTI, FileName)).AsBoolean
        Else
            Log("UTI not supported")
            Return False
        End If
    Else
        Log("Cannot send attachments.")
        Return False
    End If
End Sub

Use File.ReadBytes instead of "abc".GetBytes('utf8")

The file name is only used to set the name in the message.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Erel, thank you!
 
Upvote 0
Top