Jagprit
Jagprit

Reputation: 41

Sending Attchaments using MailCore2 on ios 8

I am not able to send attachments through MailCore2. Can I use MailCore for ios 8?. Can you please provide me the proper approach for it. I want to send docx,pdf,xls,png and jpeg files in attachments

Upvotes: 3

Views: 2455

Answers (3)

andrea.rinaldi
andrea.rinaldi

Reputation: 1187

With Swift 4 I had to write this code:

if let imageData = self.getImageData(filename: "pic.jpg") {
    var attachment = MCOAttachment()
    attachment.mimeType =  "image/jpg"
    attachment.filename = "image.jpg"
    attachment.data = imageData
    builder.addAttachment(attachment)
}

NSData is no more usable.

Upvotes: 1

Alessandro Mattiuzzi
Alessandro Mattiuzzi

Reputation: 2467

To send an image as attachment in SWIFT just add:

    var dataImage: NSData?
    dataImage = UIImageJPEGRepresentation(image, 0.6)!
    var attachment = MCOAttachment()
    attachment.mimeType =  "image/jpg"
    attachment.filename = "image.jpg"
    attachment.data = dataImage
    builder.addAttachment(attachment)

Upvotes: 1

emotality
emotality

Reputation: 13045

Loop through all attachments (file paths) and add them to the MessageBuilder:

MCOMessageBuilder *msgBuilder = [[MCOMessageBuilder alloc] init];

NSArray *allAttachments = @[@"/var/mobile/etc..", @"/var/mobile/etc2.."];

for (int x = 0; x < allAttachments.count; x++) {
    NSString *attachmentPath = allAttachments[x];
    MCOAttachment *attachment = [MCOAttachment attachmentWithContentsOfFile:attachmentPath];
    [msgBuilder addAttachment:attachment];
}

Example of the expected attachment path:

"/var/mobile/Containers/Data/Application/B47D8576-B38E-4864-AC76-8EBC630B6B44/Documents/IMG_0522.JPG"

Upvotes: 6

Related Questions