Repository navigation
5. Not All Devices Can Send Emails
A device might be capable of sending emails, but it might not as well. Normally, this case should be treated properly and not to be left unhandled, otherwise the app will be crashing every time users try to write an email. However, EmailComposer ensures that this is not going to happen.
More particularly, EmailComposer checks internally if the device can actually send emails or not, and triggers the presentation of the email composer if only the device is capable of sending emails. In the opposite case, there are two things that occur:
- If you handle the email composing results, then you should expect to get the
EmailComposerResult/deviceCannotSendEmailsas the returned value. - Instead of the email composer controller, a default view is displayed in order to notify the user. That view contains a Text view with a related message, and a button in order to dismiss the modal sheet.
That default view is the following:
struct DeviceCannotSendEmailsDefaultView: View {
@Environment(\.presentationMode) private var presentationMode
var body: some View {
VStack {
Text("Unable to send emails from this device.")
.padding(.bottom, 20)
.multilineTextAlignment(.center)
Button("Dismiss") {
presentationMode.wrappedValue.dismiss()
}
}
}
}Even though the above view will perfectly work, consider it as a placeholder. You are adviced to provide a customized, and probably localized view instead in order to fit to the look and feel of your app.
To achieve that, in the emailComposer view modifier pass an instance of the DeviceCannotSendEmailsView view. Supply any SwiftUI view or a combination of primitive views as the content you want to display in case the device cannot send emails.
Button("Send email") { ... }
.emailComposer(isPresented: $showEmailComposer,
deviceCannotSendEmailsView: DeviceCannotSendEmailsView(content: {
// Your custom view here...
}))If you don't want to display anything, then do as above once again, but this time provide an EmptyView as the content of the DeviceCannotSendEmailsView view.
Button("Send email") { ... }
.emailComposer(isPresented: $showEmailComposer,
deviceCannotSendEmailsView: DeviceCannotSendEmailsView(content: {
EmptyView()
}))Notice that it's your duty to dismiss the email composer, no matter if you provide a custom view or an empty view as content to the DeviceCannotSendEmailsView view. That's why it's important to handle the EmailComposerResult/deviceCannotSendEmails result.