MFMailComposeViewController在iOS 13模拟器和设备上表现不同

20

我正在尝试在应用程序中显示MFMailComposeViewController

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

在iOS 12中,它可以正常显示,无论是在模拟器还是设备上。

enter image description here

但是当我在运行相同的项目时,它在运行iOS 13的设备上看起来像这样。

enter image description here

导航栏颜色消失了。发送按钮也看不见了。
所以我添加了mailComposeViewController.navigationBar.backgroundColor = .mv_primary,但它仍然不能在设备上显示。奇怪的是,背景颜色在模拟器中显示出来了。
然而,有一个奇怪的行为。当我在模拟器中运行它时,MFMailComposeViewController立即自动关闭。

enter image description here

以下错误也会显示在Xcode控制台中。
``` [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 ```
我猜这个奇怪的dismiss错误是Xcode的一个bug。但是如何修复设备上的背景颜色和发送按钮没有显示出来呢?
这是我设置所有导航栏相关样式的方法。
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]
}

演示项目


1
我在iOS 13.3上遇到了完全相同的问题。你找到解决方法了吗? - mamba4ever
很遗憾,我没有得到回复。我也在苹果开发者论坛上发布了同样的问题,但那里也没有得到回应。 - Isuru
@Isuru,你找到任何解决方案了吗? - Ketan Odedra
@Isuru 在你的问题中提到你正在尝试在模拟器中运行它,然而这不能在模拟器中测试,你需要一个设备来测试。在iOS 13中,默认的模态呈现方式是页面表单,你可以尝试将模态呈现方式更改为全屏,看看是否会出现相同的行为?在设备上是否也有相同的响应? - NSDumb
3个回答

1
邮件编辑器立即关闭的原因是因为您无法在模拟器上发送电子邮件,这与iOS本身的实现不同。我猜测这里发生的事情是,虽然模拟器实现只使用了一些普通的UI元素,但本机iOS上的MFMailComposeViewController实际上像UIDocumentPickerViewControllerUIActivityViewController一样托管。这意味着截图和尝试遍历视图树是不可能的,因为该视图不是您应用程序的实际部分。他们这样做是因为这些控制器包含用户私有信息。托管的视图控制器不允许自定义,并且不符合全局UINavigationBar.appearance()。这就解释了为什么它会出现在模拟器上而不是您的本机设备上。

1

1
在展示之前,请添加这行代码。这样它就可以正常工作了。这是iOS 13中的一个变化。
mailController.modalPresentationStyle = .fullScreen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接