diff --git a/Example/Sources/AppDelegate.swift b/Example/Sources/AppDelegate.swift index 5266522..6816e10 100644 --- a/Example/Sources/AppDelegate.swift +++ b/Example/Sources/AppDelegate.swift @@ -20,7 +20,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate { * Configure BugShaker with an array of email recipients (required) * and an optional custom subject line to use for all bug reports. */ - BugShaker.configure(to: [ "example@email.com" ], subject: "Bug Report", body: "Hi Developers, I am reporting a bug where...") + let fileName = "testFile.txt" + let fileContent = "This is the content of the file." + let file = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent(fileName) + + try! fileContent.write(to: file, atomically: true, encoding: .utf8) + + let attachment = BugShaker.MailAttachment(data: try! Data(contentsOf: file), mimeType: "text/plain", fileName: fileName) + + BugShaker.configure(to: [ "example@email.com" ], subject: "Bug Report", body: "Hi Developers, I am reporting a bug where...", attachments: [attachment]) return true } diff --git a/Podfile.lock b/Podfile.lock index 1f565fe..6dd3946 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,20 +1,20 @@ PODS: - - Nimble (8.0.1) - - Quick (2.1.0) + - Nimble (8.0.4) + - Quick (2.2.0) DEPENDENCIES: - Nimble - Quick SPEC REPOS: - https://github.com/cocoapods/specs.git: + https://github.com/CocoaPods/Specs.git: - Nimble - Quick SPEC CHECKSUMS: - Nimble: 45f786ae66faa9a709624227fae502db55a8bdd0 - Quick: 4be43f6634acfa727dd106bdf3929ce125ffa79d + Nimble: 18d5360282923225d62b09d781f63abc1a0111fc + Quick: 7fb19e13be07b5dfb3b90d4f9824c855a11af40e PODFILE CHECKSUM: 670d452da847b95b324bec94807b831678d715b0 -COCOAPODS: 1.7.1 +COCOAPODS: 1.8.4 diff --git a/Sources/BugShaker.swift b/Sources/BugShaker.swift index 679edc3..36bd071 100644 --- a/Sources/BugShaker.swift +++ b/Sources/BugShaker.swift @@ -18,6 +18,19 @@ final public class BugShaker { static var recipients: [String]? static var subject: String? static var body: String? + static var attachments: (() -> [MailAttachment])? + } + + public struct MailAttachment { + let data: Data + let mimeType: String + let fileName: String + + public init(data: Data, mimeType: String, fileName: String) { + self.data = data + self.mimeType = mimeType + self.fileName = fileName + } } // MARK: - Configuration @@ -30,10 +43,11 @@ final public class BugShaker { - subject: Custom subject line to use for the report email. - body: Custom email body (plain text). */ - public class func configure(to recipients: [String], subject: String?, body: String? = nil) { + public class func configure(to recipients: [String], subject: String?, body: String? = nil, attachments: (() -> [MailAttachment])? = nil) { Config.recipients = recipients Config.subject = subject Config.body = body + Config.attachments = attachments } } @@ -47,7 +61,10 @@ extension UIViewController: MFMailComposeViewControllerDelegate { } override open func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { - guard BugShaker.isEnabled && motion == .motionShake else { return } + guard BugShaker.isEnabled && motion == .motionShake else { + next?.motionEnded(motion, with: event) + return + } let cachedScreenshot = captureScreenshot() @@ -127,6 +144,10 @@ extension UIViewController: MFMailComposeViewControllerDelegate { if let screenshot = screenshot, let screenshotJPEG = screenshot.jpegData(compressionQuality: CGFloat(1.0)) { mailComposer.addAttachmentData(screenshotJPEG, mimeType: "image/jpeg", fileName: "screenshot.jpeg") } + + BugShaker.Config.attachments?().forEach { + mailComposer.addAttachmentData($0.data, mimeType: $0.mimeType, fileName: $0.fileName) + } present(mailComposer, animated: true, completion: nil) }