0

I am working on an app that has an email form which holds all the usual contact information.

I need to be able to format the email body with that information. I have done it successfully in the Android sister app, but I can't find how to achieve it in Swift.

String message = "Name: " + txtFullName.getText().toString() + "\n" + "Street: " + txtStreet.getText().toString() + "\n" + "Suburb: " + txtSuburb.getText().toString() + "\n" + "State: " + spState.getSelectedItem().toString() + ", Post Code: " + txtPostCode.getText().toString() + "\n" + "Phone number : " + txtPhone.getText().toString() + "\n\n" +

"Have you ever tried before? " + sp_ever_tried.getSelectedItem().toString() + "\n\n" + "Would you be intereseted in Hosting a Facebook Party? " + sp_hos_fb_party.getSelectedItem().toString() + "\n\n" + "Would you like to become a Consultant? " + sp_consultant_joiner.getSelectedItem().toString() + "\n\n" + "Comments: \n" + txtComments.getText().toString();

Apologies about the code formatting in here.

in Swift I have :

@IBOutlet var txtFullName: UITextField!
@IBOutlet var txtAddress: UITextField!
@IBOutlet var txtSuburb: UITextField!
@IBOutlet var txtPostCode: UITextField!
@IBOutlet var txtPhoneNumber: UITextField!
@IBOutlet var txtEmailAddresss: UITextField!
@IBOutlet var txtComments: UITextView!
@IBOutlet var txtTriedB4: UITextField!
@IBOutlet var txtState: UITextField!
@IBOutlet var txtHostFBParty: UITextField!
@IBOutlet var txtJoinAsCons: UITextField!

Then in

@IBAction sendEmail(sender: AnyObject) {

       ...
      let msgBody = "<h3>Free Sample Request</H3>\n"
        "<b>Full Name: " + txtFullName.text! + "</b>\n"
        ...
    }

But I'm stuck here building the message body.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Nick
  • 1
  • 4
  • Oh.and yes I get that in the android code it is not HTML as in the Swift code and in the swift code I should be using
    for new lines.
    – Nick Feb 27 '16 at 16:10

1 Answers1

0

You could do this.

let msgBody = "Free Sample Request\n" "Full Name: \(txtFullName.text!)\n" + ...

I'd also recommend unwrapping your optionals before creating the message body using if let or guard let to avoid crashing if any of the fields are nil.

D. Greg
  • 991
  • 1
  • 9
  • 21