Hack MFMailComposeViewController because I really really want it to work

This commit is contained in:
James Magahern
2021-05-25 15:38:37 -07:00
parent 22652ee05f
commit 2f40785b50
3 changed files with 84 additions and 4 deletions

View File

@@ -51,6 +51,12 @@ class BrowserViewController: UIViewController
}
}
internal enum PreferredEmailSharingRecipient: String, CaseIterable {
case readLater = "Read Later"
case bookmark = "Bookmark"
case other = "Other…"
}
override var preferredStatusBarStyle: UIStatusBarStyle { .lightContent }
internal var changingFocusToAutocompleteController = false
@@ -270,7 +276,7 @@ class BrowserViewController: UIViewController
// Email
documentControls.emailView.addAction(UIAction { [unowned self] _ in
composeEmailForCurrentURL(fromViewController: documentControls)
queryPreferredEmailSharingRecipientForCurrentURL(fromViewController: documentControls)
}, for: .touchUpInside)
// Settings
@@ -324,7 +330,24 @@ class BrowserViewController: UIViewController
self.present(activityController, animated: true, completion: nil)
}
internal func composeEmailForCurrentURL(fromViewController: UIViewController?) {
internal func queryPreferredEmailSharingRecipientForCurrentURL(fromViewController: UIViewController?) {
if let fromViewController = fromViewController {
fromViewController.dismiss(animated: false, completion: nil)
}
let actionSheet = UIAlertController(title: "Select Preferred Recipient", message: nil, preferredStyle: .actionSheet)
for preferredRecipient in PreferredEmailSharingRecipient.allCases {
actionSheet.addAction(UIAlertAction(title: preferredRecipient.rawValue, style: .default, handler: { [unowned self] action in
actionSheet.dismiss(animated: true, completion: { [unowned self] in
composeEmailForCurrentURL(preferredRecipientSelection: preferredRecipient)
})
}))
}
present(actionSheet, animated: true, completion: nil)
}
internal func composeEmailForCurrentURL(preferredRecipientSelection: PreferredEmailSharingRecipient) {
guard let url = self.webView.url else { return }
let composeController = MFMailComposeViewController()
@@ -332,10 +355,22 @@ class BrowserViewController: UIViewController
composeController.setMessageBody(url.absoluteString, isHTML: false)
composeController.mailComposeDelegate = self
if let fromViewController = fromViewController {
fromViewController.dismiss(animated: false, completion: nil)
let preferredRecipient: String? = { preferredRecipientSelection in
switch preferredRecipientSelection {
case .readLater:
return "read@buzzert.net"
case .bookmark:
return "bookmarks@buzzert.net"
case .other:
return nil
}
}(preferredRecipientSelection)
if let preferredRecipient = preferredRecipient {
composeController.setToRecipients([ preferredRecipient ])
}
present(composeController, animated: true, completion: nil)
}