Isuru
Isuru

Reputation: 31323

MFMailComposeViewController behaves differently in iOS 13 simulator and device

I'm trying to display MFMailComposeViewController in an app.

if MFMailComposeViewController.canSendMail() {
    let mailComposeViewController = MFMailComposeViewController()
    mailComposeViewController.navigationBar.tintColor = .white
    mailComposeViewController.mailComposeDelegate = self
    mailComposeViewController.setToRecipients(["[email protected]"])
    mailComposeViewController.setSubject("Feedback")
    present(mailComposeViewController, animated: true)
} else {
    print("This device is not configured to send email. Please set up an email account.")
}

In iOS 12, it shows up without an issue. In both simulator and device.

enter image description here

But when I run the same project on a device running iOS 13, it looks like this.

enter image description here

The navigation bar color is gone. Also the send button is also invisible.

So I added mailComposeViewController.navigationBar.backgroundColor = .mv_primary but it still doesn't show on the device. Strangely the background color shows in the simulator.

However there's a strange behavior. The MFMailComposeViewController immediately dismisses by itself when I run it in the simulator.

enter image description here

The following error also shows up in the Xcode console.

[Common] [FBSSystemService][0x5f27] Error handling open request for com.apple.MailCompositionService: { userInfo = { FBSOpenApplicationRequestID = 0x5f27; } underlyingError = ; } 2019-11-01 14:40:05.214158+0530 MailCompose[11289:262267] [Assert] Connection request invalidated without resuming our _serviceSessionConnection. This is an error. 2019-11-01 14:40:05.216901+0530 MailCompose[11289:262054] [General] #CompositionServices _serviceViewControllerReady: NSError Domain=_UIViewServiceInterfaceErrorDomain Code=0

I guess the weird dismiss error is a Xcode bug. But how do I fix the background color and the send button not showing up in the device?

This is how I set all the navigationbar related styles.

UINavigationBar.appearance().barTintColor = .mv_primary
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
if #available(iOS 11.0, *) {
    UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
}

Demo project

Upvotes: 20

Views: 1863

Answers (3)

Hiro
Hiro

Reputation: 400

This is new UI Style from iOS 13. You can disable it in Storyboard or set manual. Presenting modal in iOS 13 fullscreen

Upvotes: 1

lennartk
lennartk

Reputation: 619

The reason why the Mail composer dismisses immediately is because you can't actually send an email from the simulator. The implementation is different from iOS itself.

What I guess is happening here is that while the simulator implementation uses just some normal UI elements, the MFMailComposeViewController on native iOS is actually hosted like UIDocumentPickerViewController or UIActivityViewController. This means screenshots and trying to traverse the view tree is impossible, because the view is not an actual part of your application. They do that because these controllers contain user private information. Hosted view controller do NOT allow for customization, and do not comply with your global UINavigationBar.appearance(). This would explain why it does show up in the simulator and not on your native device.

Upvotes: 1

King.lbt
King.lbt

Reputation: 881

Add this line of code before present it. It will work normal. This a change in iOS 13

mailController.modalPresentationStyle = .fullScreen

Upvotes: 1

Related Questions