Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added EmailUtil implementation. Improvement in MochaTask class.
- Loading branch information
Gregory Sholl e Santos
committed
Jul 19, 2017
1 parent
6aa0183
commit 9fa54bb
Showing
8 changed files
with
227 additions
and
32 deletions.
There are no files selected for viewing
Binary file modified
BIN
+1.54 KB
(100%)
...aUtilities.xcworkspace/xcuserdata/gregorysholl.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
MochaUtilities/Classes/Network/Domain/MochaEmailAttachment.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// MochaEmailAttachment.swift | ||
// Pods | ||
// | ||
// Created by Gregory Sholl e Santos on 18/07/17. | ||
// | ||
// | ||
|
||
import UIKit | ||
|
||
//MARK: - | ||
|
||
public class MochaEmailAttachment { | ||
|
||
//MARK: Variables | ||
|
||
private var _data : Data! | ||
private var _type : String! | ||
private var _filename: String! | ||
|
||
public var data : Data { | ||
return _data | ||
} | ||
|
||
public var type : String { | ||
return _type | ||
} | ||
|
||
public var filename : String { | ||
return _filename | ||
} | ||
|
||
//MARK: Inits | ||
|
||
///compressionQuality is a number between 0.0 and 1.0 | ||
public init(jpegImage: UIImage, compressionQuality: CGFloat = 1.0, filename: String) { | ||
_data = UIImageJPEGRepresentation(jpegImage, compressionQuality) | ||
_type = "image/jpeg" | ||
_filename = filename | ||
} | ||
|
||
public init(pngImage: UIImage, filename: String) { | ||
_data = UIImagePNGRepresentation(pngImage) | ||
_type = "image/png" | ||
_filename = filename | ||
} | ||
|
||
public init(data: Data, type: String, filename: String) { | ||
_data = data | ||
_type = type | ||
_filename = filename | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
MochaUtilities/Classes/Network/Protocols/EmailUtilDelegate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// EmailUtilDelegate.swift | ||
// Pods | ||
// | ||
// Created by Gregory Sholl e Santos on 18/07/17. | ||
// | ||
// | ||
|
||
import UIKit | ||
|
||
public protocol EmailUtilDelegate { | ||
func onEmailSuccessful() | ||
func onEmailCancelled() | ||
func onEmailFailed() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// | ||
// EmailUtil.swift | ||
// Pods | ||
// | ||
// Created by Gregory Sholl e Santos on 18/07/17. | ||
// | ||
// | ||
|
||
import UIKit | ||
|
||
import MobileCoreServices | ||
import MessageUI | ||
|
||
//MARK: - Main | ||
|
||
public class EmailUtil: NSObject { | ||
|
||
//MARK: Variables | ||
|
||
static fileprivate var instance : EmailUtil? | ||
|
||
fileprivate var delegate : EmailUtilDelegate? | ||
|
||
//MARK: Inits | ||
|
||
fileprivate override init() { | ||
super.init() | ||
} | ||
} | ||
|
||
//MARK: - Valid | ||
|
||
public extension EmailUtil { | ||
|
||
static public func isValid(_ string: String?, strictRules strict: Bool = false) -> Bool { | ||
let emailRegex = strict ? "^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$" : "^.+@.+\\.[A-Za-z]{2}[A-Za-z]*$" | ||
let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex) | ||
return emailTest.evaluate(with: string) | ||
} | ||
} | ||
|
||
//MARK: - Send | ||
|
||
public extension EmailUtil { | ||
|
||
static public func canSend() -> Bool { | ||
return MFMailComposeViewController.canSendMail() | ||
} | ||
|
||
static public func send(to destinationEmail: String, withSubject subject: String = "", andBody body: String = "") { | ||
|
||
if !canSend() { | ||
return | ||
} | ||
|
||
var email = "mailto:\(destinationEmail)" | ||
|
||
if subject.isNotEmpty { | ||
email = "\(email)?subject=\(subject)" | ||
} | ||
|
||
if body.isNotEmpty { | ||
email = "\(email)&body=\(body)" | ||
} | ||
|
||
if let addedPercent = email.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) { | ||
if let url = URL(string: addedPercent) { | ||
UIApplication.shared.openURL(url) | ||
} | ||
} | ||
} | ||
|
||
static public func open(with delegate: EmailUtilDelegate, on viewController: UIViewController, withRecipients recipients: [String] = [], subject: String = "", body: String = "", isBodyHtml: Bool = false, andAttachments attachments: [MochaEmailAttachment] = []) -> EmailUtil? { | ||
|
||
if !canSend() { | ||
return nil | ||
} | ||
|
||
let emailUtils = EmailUtil() | ||
emailUtils.delegate = delegate | ||
|
||
let mailCompose = MFMailComposeViewController() | ||
mailCompose.mailComposeDelegate = emailUtils | ||
mailCompose.setToRecipients(recipients) | ||
mailCompose.setSubject(subject) | ||
mailCompose.setMessageBody(body, isHTML: isBodyHtml) | ||
|
||
for attachment in attachments { | ||
mailCompose.addAttachmentData(attachment.data, mimeType: attachment.type, fileName: attachment.filename) | ||
} | ||
|
||
viewController.present(mailCompose, animated: true, completion: nil) | ||
|
||
EmailUtil.instance = emailUtils | ||
|
||
return emailUtils | ||
} | ||
} | ||
|
||
//MARK: - Mail Compose View Controller Delegate | ||
|
||
extension EmailUtil: MFMailComposeViewControllerDelegate { | ||
|
||
public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { | ||
EmailUtil.instance = nil | ||
|
||
controller.dismiss(animated: true, completion: { | ||
DispatchQueue.main.async { | ||
switch result { | ||
case .cancelled: | ||
self.delegate?.onEmailCancelled() | ||
case .sent: | ||
self.delegate?.onEmailSuccessful() | ||
default: | ||
break | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters