var mailComposeDelegate : MFMailComposeViewControllerDelegate ?
protocol MFMailComposeViewControllerDelegate
iOS, iPadOS
class MFMailComposeViewController
Mac Catalyst, visionOS
@MainActor
class MFMailComposeViewController
Use this view controller to display a standard email interface inside your app. Before presenting the interface, populate the fields with initial values for the subject, email recipients, body text, and attachments of the email. After presenting the interface, the person can edit your initial values before sending the email.
The composition interface doesn’t guarantee the delivery of your email message; it only lets you construct the initial message and present it for user approval. The person may opt to cancel the composition interface which discards the message and its contents. If the person opts to send the message, the message queues in the user’s Mail app outbox. The Mail app is ultimately responsible for sending the message.
Important
You must not modify the view hierarchy presented by this view controller. However, you can customize the appearance of the interface using the UIAppearance
protocol.
An alternate way to compose emails is to create and open a URL that uses the mailto
scheme. URLs of that type go directly to the built-in Mail app, which uses your URL to configure a message. For information about the structure of mailto
URLs, see Apple URL Scheme Reference.
Before presenting the mail compose view controller, always call the can
method to see if the person configured the current device to send email. If the person’s device isn’t set up for the delivery of email, you can notify the person or disable the email dispatch features in your application. You shouldn’t attempt to use this interface if the can
method returns false
.
if !MFMailComposeViewController.canSendMail() {
print("Mail services are not available")
return
}
if (![MFMailComposeViewController canSendMail]) {
NSLog(@"Mail services are not available.");
return;
}
After verifying that mail services are available, you can create and configure the mail composition view controller and then present it as any other view controller. Use the methods of this class to specify the subject, recipients, and message body of the email, including any attachments you want to send with the message. The sample code below shows how to configure the composition interface and present it modally. Always assign a delegate to the mail
property, because the delegate is responsible for dismissing the composition interface later.
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
composeVC.setToRecipients(["address@example.com"])
composeVC.setSubject("Hello!")
composeVC.setMessageBody("Hello from California!", isHTML: false)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
MFMailComposeViewController* composeVC = [[MFMailComposeViewController alloc] init];
composeVC.mailComposeDelegate = self;
// Configure the fields of the interface.
[composeVC setToRecipients:@[@"address@example.com"]];
[composeVC setSubject:@"Hello!"];
[composeVC setMessageBody:@"Hello from California!" isHTML:NO];
// Present the view controller modally.
[self presentViewController:composeVC animated:YES completion:nil];
Important
After presenting a mail compose view controller, the system ignores any attempts to modify the email using the methods of this class. The user can still edit the content of the email, but your app can’t. Therefore, always configure the fields of your email before presenting the view controller.
The mail compose view controller isn’t dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mail
method of its delegate. Your implementation of that method must dismiss the view controller explicitly, as shown in sample code below. You can also use this method to check the result of the operation.
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
[self dismissViewControllerAnimated:YES completion:nil];
}
The user can delete a queued message before it’s sent. Although the view controller reports the success or failure of the operation to its delegate, this class doesn’t provide a way for you to verify if the email sent.
For more information on how to present and dismiss view controllers, see View Controller Programming Guide for iOS.
var mailComposeDelegate : MFMailComposeViewControllerDelegate ?
protocol MFMailComposeViewControllerDelegate
class func canSendMail () -> Bool
func setSubject (String)
func setToRecipients ([String]?)
func setCcRecipients ([String]?)
func setBccRecipients ([String]?)
func setMessageBody (String, isHTML : Bool)
func addAttachmentData (Data, mimeType : String, fileName : String)
func setPreferredSendingEmailAddress (String)
struct MFMailComposeError
let MFMailComposeErrorDomain : String
CVarArg
CustomDebugStringConvertible
CustomStringConvertible
Equatable
Hashable
NSCoding
NSExtensionRequestHandling
NSObjectProtocol
NSTouchBarProvider
UIActivityItemsConfigurationProviding
UIAppearanceContainer
UIContentContainer
UIFocusEnvironment
UIPasteConfigurationSupporting
UIResponderStandardEditActions
UIStateRestoring
UITraitChangeObservable
UITraitEnvironment
UIUserActivityRestoring