1 // Copyright 2022-present 650 Industries. All rights reserved.
2 
3 import MessageUI
4 import MobileCoreServices
5 import ABI49_0_0ExpoModulesCore
6 
7 public class MailComposerModule: Module {
8   var currentSession: MailComposingSession?
9 
definitionnull10   public func definition() -> ModuleDefinition {
11     Name("ExpoMailComposer")
12 
13     AsyncFunction("isAvailableAsync", canSendMail)
14 
15     AsyncFunction("composeAsync") { (options: MailComposerOptions, promise: Promise) in
16       guard canSendMail() else {
17         throw CannotSendMailException()
18       }
19       guard self.currentSession == nil else {
20         throw OperationInProgressException()
21       }
22       guard let appContext = self.appContext else {
23         throw AppContextLostException()
24       }
25 
26       let session = MailComposingSession(appContext)
27       try session.compose(options: options)
28 
29       // This is important to retain the session until it finishes
30       self.currentSession = session
31 
32       session.presentViewController { result in
33         promise.settle(with: result)
34 
35         // Release the session so it can get deallocated
36         self.currentSession = nil
37       }
38     }
39     .runOnQueue(.main)
40   }
41 }
42 
canSendMailnull43 private func canSendMail() -> Bool {
44   return MFMailComposeViewController.canSendMail()
45 }
46