1

I have a button that is right now opening the mail application and adding a contact through a property I set up. How can I do this to also add a subject line through a property?

- (IBAction)tourButton:(id)sender {

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@", self.displayEmail]]];

}
Packy
  • 3,405
  • 9
  • 50
  • 87

2 Answers2

7

Try this:

- (IBAction)tourButton:(id)sender {

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@?subject=%@", self.displayEmail, @"mail_subject_here"]]];

}

Aslo you could add other parameters:
cc: &cc=cc_mail@example.com
body: &body=mail_body

for iOS 11.0 and above. Swift 4.2 +

let emailStr = "\(EMAIL_TO)?subject=\(Email_Subject)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
if let url = URL(string: "mailto:\(emailStr ?? "")") {
       UIApplication.shared.open(url)
}
Mohit Kumar
  • 2,898
  • 3
  • 21
  • 34
Vlad Papko
  • 13,184
  • 4
  • 41
  • 57
0

You should use MFMailComposeViewController.

  • (void)setSubject:(NSString*)subject

The text to display in the subject line.

This method replaces the previous subject text with the new text. You should call this method before you display the mail composition interface only. Do not call it after presenting the interface to the user.

Availability Available in iOS 3.0 and later.

source: https://developer.apple.com/library/ios/documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html#//apple_ref/occ/instm/MFMailComposeViewController/setSubject:

soprof
  • 326
  • 2
  • 6
  • Thanks. I just went with the MFMailComposeViewController to set everything. Plus it opens the email from within the app. – Packy Apr 25 '14 at 17:45