Hack MFMailComposeViewController because I really really want it to work
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
33
App/Hacks/MFMailComposeViewControllerFix.m
Normal file
33
App/Hacks/MFMailComposeViewControllerFix.m
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// MFMailComposeViewControllerFix.c
|
||||
// App
|
||||
//
|
||||
// Created by James Magahern on 5/25/21.
|
||||
//
|
||||
|
||||
#import <CoreFoundation/CoreFoundation.h>
|
||||
#import <MessageUI/MessageUI.h>
|
||||
#import <objc/runtime.h>
|
||||
|
||||
@interface MFMailComposeViewController (InternalMethods)
|
||||
+ (BOOL)canSendMailSourceAccountManagement:(NSInteger)sourceAccountManagement;
|
||||
@end
|
||||
|
||||
static void swizzleClassMethod(Class targetClass, SEL originalSelector, IMP replacementIMP)
|
||||
{
|
||||
Method originalMethod = class_getClassMethod(targetClass, originalSelector);
|
||||
method_setImplementation(originalMethod, replacementIMP);
|
||||
}
|
||||
|
||||
__attribute__((constructor))
|
||||
static void swizzleMFMailComposeViewController()
|
||||
{
|
||||
// Hacks to work around the fact that MFMailComposeViewController is broken in iOS 14+
|
||||
swizzleClassMethod(MFMailComposeViewController.class, @selector(canSendMail), imp_implementationWithBlock(^BOOL(void) {
|
||||
return YES;
|
||||
}));
|
||||
|
||||
swizzleClassMethod(MFMailComposeViewController.class, @selector(canSendMailSourceAccountManagement:), imp_implementationWithBlock(^BOOL(NSInteger sam) {
|
||||
return YES;
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user