Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MFMailComposeViewController Support #56

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Xcode/Closures.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
3FB77F311EE4DB5200C60F5C /* UITableViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FB77F301EE4DB5200C60F5C /* UITableViewTests.swift */; };
3FD393C61F9E583900DA155D /* UIBarButtonItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FD393C51F9E583900DA155D /* UIBarButtonItem.swift */; };
3FD393C81F9E61A900DA155D /* UIBarButtonItemTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FD393C71F9E61A900DA155D /* UIBarButtonItemTests.swift */; };
CC095542241EA9C0000125E1 /* MFMailComposeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC095541241EA9C0000125E1 /* MFMailComposeViewController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -63,6 +64,7 @@
3FB77F301EE4DB5200C60F5C /* UITableViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITableViewTests.swift; sourceTree = "<group>"; };
3FD393C51F9E583900DA155D /* UIBarButtonItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIBarButtonItem.swift; sourceTree = "<group>"; };
3FD393C71F9E61A900DA155D /* UIBarButtonItemTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIBarButtonItemTests.swift; sourceTree = "<group>"; };
CC095541241EA9C0000125E1 /* MFMailComposeViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MFMailComposeViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -89,6 +91,7 @@
children = (
3F59C5681F1C32AA00945666 /* Core.swift */,
3F59C5691F1C32AA00945666 /* KVO.swift */,
CC095541241EA9C0000125E1 /* MFMailComposeViewController.swift */,
3F59C56A1F1C32AA00945666 /* UICollectionView.swift */,
3F59C56B1F1C32AA00945666 /* UIControl.swift */,
3F59C56C1F1C32AA00945666 /* UIGestureRecognizer.swift */,
Expand Down Expand Up @@ -258,6 +261,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
CC095542241EA9C0000125E1 /* MFMailComposeViewController.swift in Sources */,
3F59C5731F1C32BF00945666 /* UICollectionView.swift in Sources */,
3F59C5741F1C32BF00945666 /* UIControl.swift in Sources */,
3F59C5721F1C32BF00945666 /* KVO.swift in Sources */,
Expand Down
87 changes: 87 additions & 0 deletions Xcode/Closures/Source/MFMailComposeViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// MFMailComposeViewController.swift
//
//
// Created by Alex Ivashko on 15.03.2020.
//

import MessageUI

@available(iOS 9.0, *)
class MailComposeDelegate: NSObject, MFMailComposeViewControllerDelegate, DelegateProtocol {
fileprivate static var delegates = Set<DelegateWrapper<MFMailComposeViewController, MailComposeDelegate>>()

fileprivate var didFinishWith: ((_ result: MFMailComposeResult, _ error: Error?) -> Void)?
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
didFinishWith?(result, error)
}

override func responds(to aSelector: Selector!) -> Bool {
switch aSelector {
case #selector(MailComposeDelegate.mailComposeController(_:didFinishWith:error:)):
return didFinishWith != nil
default:
return super.responds(to: aSelector)
}
}
}

@available(iOS 9.0, *)
extension MFMailComposeViewController {
public convenience init?(subject: String? = nil,
toRecipients: [String]? = nil,
ccRecipients: [String]? = nil,
bccRecipients: [String]? = nil,
body: (body: String, isHTML: Bool)? = nil,
attachment: (data: Data, mimeType: String, fileName: String)? = nil,
handler: @escaping ((_ result: MFMailComposeResult, _ error: Error?) -> Void)) {
guard MFMailComposeViewController.canSendMail() else {
return nil
}
self.init()
if let subject = subject {
setSubject(subject)
}
setToRecipients(toRecipients)
setCcRecipients(ccRecipients)
setBccRecipients(bccRecipients)
if let body = body {
setMessageBody(body.body, isHTML: body.isHTML)
}
if let attachment = attachment {
addAttachmentData(attachment.data, mimeType: attachment.mimeType, fileName: attachment.fileName)
}
didFinish(handler: handler)
}

@discardableResult
public func didFinish(handler: @escaping (_ result: MFMailComposeResult, _ error: Error?) -> Void) -> Self {
update { $0.didFinishWith = handler }
}
}

@available(iOS 9.0, *)
extension MFMailComposeViewController: DelegatorProtocol {
@discardableResult
@objc func update(handler: (_ delegate: MailComposeDelegate) -> Void) -> Self {
DelegateWrapper.update(
self,
delegate: MailComposeDelegate(),
delegates: &MailComposeDelegate.delegates,
bind: MFMailComposeViewController.bind
) {
handler($0.delegate)
}
return self
}

public func clearClosureDelegates() {
DelegateWrapper.remove(delegator: self, from: &MailComposeDelegate.delegates)
MFMailComposeViewController.bind(self, nil)
}

fileprivate static func bind(_ delegator: MFMailComposeViewController, _ delegate: MailComposeDelegate?) {
delegator.mailComposeDelegate = nil
delegator.mailComposeDelegate = delegate
}
}
2 changes: 1 addition & 1 deletion Xcode/ClosuresTests/UITableViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class UITableViewTests: XCTestCase {
func testTableViewMethodExpectations() {
let lastKnownDelegateCount: Int
if #available(iOS 13, *) {
lastKnownDelegateCount = 49
lastKnownDelegateCount = 48
} else {
lastKnownDelegateCount = 41
}
Expand Down