2

I'm using the code provided in answer 2 of SwiftUI: Send email and it works fine as is. However, in my app, I want to set the vc.setSubject to include some variables:

vc.setSubject("2020_\(riderFlagNumber)_\(activeBonus.category)_\(activeBonus.city)_\(activeBonus.state),\(activeBonus.code)")

riderFlagNumber comes from userDefaults and works fine; but the ones marked as activeBonus are coming from the navigation detail view that triggers the email. They come from an ObservableObject that appears to only be accessible from a view. How do I pass these variables from that view into the MFMailComposeViewController()?

DJFriar
  • 340
  • 7
  • 21

1 Answers1

2

I assume the view you specify that contains ObservableObject class looks like below

class activeBonus: ObservableObject {
    @Published var category: String = "ABC"
    @Published var city: String = "Seoul"
}

If so, in the file that contains MFMailComposeViewController() add the following (@ObserverObject line specified in the below code):

struct MailView: UIViewControllerRepresentable {

    @Environment(\.presentationMode) var presentation
    @Binding var result: Result<MFMailComposeResult, Error>?
    @ObservedObject var activeBonus = activeBonus()

Once the line with @ObservedObject is added, the subject line you have mentioned will work.

Subha Narayanan
  • 442
  • 1
  • 4
  • 16