I'm generating a PDF using jinja in the background, and then want to email that through the iOS email composer after a button press in the UI. The following code works if I call it without the UI, and if I call view.close() first before calling the mail composer function. But otherwise the mail composer seems to close in the background instantly (without an error) and I never see it. I'm trying to do this in fullscreen mode, though it doesn't seem to make a difference.
Alternatively, is there a way to relaunch the view after the composer is dismissed? When I try to do so it says the view is already being presented even though I've called close().
Call from script:
show_mail_sheet('firstname.lastname@example.org', 'Test', [reference to PDF], 'text/pdf')
# - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error def mailComposeController_didFinishWithResult_error_(_self, _cmd, controller, result, error): print('Mail composer finished') # Wrap the controller parameter in an `ObjCInstance`, so we can send messages: mail_vc = ObjCInstance(controller) # Set delegate to nil, and release its memory: mail_vc.setMailComposeDelegate_(None) ObjCInstance(_self).release() # Dismiss the sheet: mail_vc.dismissViewControllerAnimated_completion_(True, None) methods = [mailComposeController_didFinishWithResult_error_] protocols = ['MFMailComposeViewControllerDelegate'] MyMailComposeDelegate = create_objc_class('MyMailComposeDelegate', NSObject, methods=methods, protocols=protocols) @on_main_thread def show_mail_sheet(recipient, subject, filename, mimetype): print("Showing mail sheet") load_framework('MessageUI') MFMailComposeViewController = ObjCClass('MFMailComposeViewController') mail_composer = MFMailComposeViewController.alloc().init().autorelease() # Use our new delegate class: delegate = MyMailComposeDelegate.alloc().init() mail_composer.setMailComposeDelegate_(delegate) mail_composer.setToRecipients([recipient]) mail_composer.setSubject(subject) filepath = os.path.join(os.getcwd(), filename) mail_composer.addAttachmentData_mimeType_fileName_(NSData.dataWithContentsOfFile_(filepath), mimetype, filename ) # Present the mail sheet: root_vc = UIApplication.sharedApplication().keyWindow().rootViewController() root_vc.presentViewController_animated_completion_(mail_composer, True, None)```