Skip to content

Modernize email and file sharing using platform share sheets #9

@jimmckeeth

Description

@jimmckeeth

Overview

Lab 12 implements email with file attachments using a custom wwEmailWithAttachment.pas unit that bridges to iOS MessageUI framework Objective-C APIs and Android Intent-based email. Both approaches have changed on modern OS versions and the iOS implementation requires manual SDK framework configuration. A modern replacement using the platform share sheet (UIActivityViewController on iOS, ACTION_SEND Intent with FileProvider on Android) is simpler, more reliable, and doesn't require native framework bridging.

Background

iOS Issues with Current Approach

  • MFMailComposeViewController (MessageUI) requires the device to have a Mail account configured. If no account is set up, the composer silently fails.
  • The MessageUI.framework must be manually added to the Delphi SDK Manager — a step that trips up training attendees and isn't necessary in modern development.
  • iOS 16+ introduced changes to MFMailComposeViewController presentation that can cause display issues if the view controller hierarchy is not handled correctly.

Android Issues with Current Approach

  • The Intent-based approach uses a bare file:// URI for the attachment, which throws FileUriExposedException on Android 7+ (API 24+). A content:// URI via FileProvider is required.
  • ACTION_SEND with EXTRA_EMAIL and EXTRA_STREAM still works but the file URI handling must be updated.

Modern Replacement

Delphi 13 (and earlier versions from XE8+) includes FMX.Share which wraps UIActivityViewController on iOS and the Android share Intent — both use the system share sheet, which lets the user choose mail, messages, AirDrop, or any other app. This:

  • Requires no iOS framework SDK additions
  • Automatically handles file URIs correctly on Android
  • Works even if no email client is configured (offers other sharing options)

Files Affected

lab-src/Lab12.../units/wwEmailWithAttachment.pas  (replace entirely)
lab-src/Lab12.../forms/formMain.pas  (call site update)
lab-src/Lab12.../FieldLogger.dproj  (remove MessageUI SDK framework entry)

Steps to Address

  1. Replace wwEmailWithAttachment.pas with a thin wrapper around FMX.Share.TShareSheet (or inline the share logic in formMain.pas):
    uses FMX.Share;
    
    procedure TFormMain.ShareReport(const FilePath: string);
    begin
      TShareSheet.Share(Self, 'Share Report', [FilePath]);
    end;
  2. Remove the MessageUI.framework entry from the Delphi SDK Manager settings documented in the lab instructions.
  3. Update the Lab 12 instructions to describe the share sheet approach and why it is preferred.
  4. Remove the Outlook COM/OLE code path for Windows (or keep it conditionally — it is still valid on desktop, but can be simplified).
  5. Verify that the HTML report file path passed to the share sheet resolves to a real temp/documents path on both platforms.
  6. Test that the share sheet receives the correct file MIME type (text/html for the report).

Test Plan

  • On iOS 17 device: tapping "Share Report" presents the system share sheet with the HTML file.
  • On iOS 17 device: choosing Mail from the share sheet opens a draft with the report attached.
  • On iOS 17 device: choosing AirDrop or Files also works (demonstrates the share sheet benefit).
  • On Android 13 emulator: tapping "Share Report" presents the Android share chooser.
  • On Android 13 emulator: no FileUriExposedException is thrown.
  • On Android 13 emulator: choosing Gmail opens a compose screen with the report attached.
  • No MessageUI framework entry is required in Delphi SDK Manager for the lab to build.
  • Lab 12 compiles and deploys without the wwEmailWithAttachment.pas unit.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions