iOS Tutorial Mails and SMS messages

Erel

Administrator
Staff member
Licensed User
MailComposer and MessageComposer types from the iPhone library allow the user to send mails and SMS messages from your app. The users will see a pre-filled form which they can modify and send.

(press on the small gear button and set the quality to HD)

Not all devices support these two features. You should check whether the feature is supported or not. In this example it is done with this code:

B4X:
mailButton.Enabled = mailc.CanSendMail
smsButton.Enabled = smsc.CanSendText
The next step is to dim and initialize MailComposer or MessageComposer, fill the fields and show the composer:
B4X:
Private Sub SendMail
   'always dim and initialize the MailComposer before you use it
   Dim mailc As MailComposer
   mailc.Initialize("mailc")
   mailc.SetToRecipients(Array("hello@example.com", "world@example.com"))
   mailc.SetSubject("This is the subject")
   mailc.SetBody("This is the <b>body</b>.", True)
   'create a file and add it as an attachment
   File.WriteString(File.DirTemp, "1.txt", "hello world")
   mailc.AddAttachment(File.DirTemp, "1.txt", "text/plain")
   mailc.Show(Page1)
End Sub
The Complete event will be raised with the result:
B4X:
Sub Mailc_Complete (Result As Int)
   If Result = mailc.RESULT_SENT Then
     hd.ToastMessageShow("Message sent", True)
   Else
     hd.ToastMessageShow("Message was not sent", True)
   End If
End Sub
The project is attached.
 

Attachments

Dan Harding

Member
Licensed User
How do you call a phone number or send an address to the map app?

I've found half of the answer - this will open a browser to display a map:

Dim addr, url As String
addr = pAddress
addr = addr.Replace(" ", "+")
url = "https://www.google.com/maps/place/" & addr
Main.App.OpenURL(url)

where pAddress is a parameter containing a street address and Main is the initial object of the app.
 
Last edited:

JanPRO

Well-Known Member
Licensed User
How do you call a phone number
B4X:
If App.CanOpenURL("tel:") = True Then
App.OpenUrl("tel:123")
End If

'OR

If App.CanOpenURL("telprompt:") = True Then
App.OpenUrl("telprompt:123")
End If
 

valentino s

Active Member
Licensed User

marcick

Well-Known Member
Licensed User
Hi Erel, could you please explain better "Not all devices support these two features. You should check whether the feature is supported or not" ?
I need to send SMS.
Can I be sure that this function will work on any device that is able to normally send SMS ?
 

marcick

Well-Known Member
Licensed User
So, only devices that doesn't have a SIM card, or devices that have SIM card but not SMS enabled (data only) will return false with smsc.CanSendText.
All other devices will be able to send SMS with MessageComposer.
Correct ?
 

wl

Well-Known Member
Licensed User
Is tehre a way to keep the mail app open so I can add info to the contents of the email ?
It seems the mail gets sent immediately ?

Tx
 

marcick

Well-Known Member
Licensed User
The email is not sent until the user click the send button, so he can add anything he likes in the body before sending.
 
Top