I have an iOS app that should allow the user to select a pdf file and send it via there email account, I have the below code that presents the user with the email and shows the pdf attached, however with the email is send the pdf is not attached to the received email.
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Public Holidays"];
NSString *plistFilePath = [[NSBundle mainBundle] pathForResource:currentCountry ofType:#"pdf"];
NSData *myData = [[NSFileManager defaultManager] contentsAtPath:plistFilePath];
[picker addAttachmentData:myData mimeType:#"application/pdf" fileName:currentCountry];
// Fill out the email body text
NSString *emailBody = #"Attached to this email is the PDF bought";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
Code is correct. You should check the filename. Is it a valid string(filename). Also presentModalViewController is deprecated. You should use presentViewController:animated:completion. Check this on device, not on simulator.
Related
I need to send a zip file via Email (or any other sharing app), and the zip file is password protected. When I download that zip file it will ask for the password. I am using SSZipArchive to zip my html file.
NSString *txtFilePath0 = [documentsDirectory stringByAppendingPathComponent:#"medical_checkups.html"];
NSArray *inputPaths = #[txtFilePath0];
[SSZipArchive createZipFileAtPath:archivePath withFilesAtPaths:inputPaths withPassword:#"123456"];
NSString *archivePath = [documentsDirectory stringByAppendingString:#"/medical_checkups.zip"];
MFMailComposeViewController *_mailController = [[MFMailComposeViewController alloc] init];
if ([MFMailComposeViewController canSendMail])
{
[_mailController setMessageBody:#""
isHTML:NO];
[_mailController setMailComposeDelegate:self];
[_mailController addAttachmentData:[NSData dataWithContentsOfFile:archivePath]
mimeType:#"application/zip"
fileName:#"medical_checkups.zip"];
[self presentViewController:_mailController animated:YES completion:nil];
}
But now when I try to unzip my zip file there is no popup for password. I also can't open my zip file.
For password protection you have to use This library https://cocoapods.org/pods/SSZipArchive
Then get your Zip file convert to NSData and Attach in Mail composer
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *WritableDBPath= [documentsDirectory stringByAppendingPathComponent:kFilename];
NSData *data = [NSData dataWithContentsOfFile:WritableDBPath];
[picker addAttachmentData:data mimeType:#"application/zip" fileName:#"/abc.zip"];
[picker setSubject:#"Database"];
[picker setMessageBody:#"Database testing" isHTML:NO];
[self presentModalViewController:picker animated:YES];
---Edited
Archive Utility
Will show you error while extracting (unzip) your file because its password protected.
So , to unzip the Your zip file outside your Application then
please use
the-unarchiver Application for Mac
---Edited
By #Priti Kanauziya,
I found the new solution . In SSZipArchive there is a new class AES and its YES by default . And we need to set it NO .
I've read posts that say the the `MFMailComposeViewController should display your attached PDF if its a 1 page document but for some reason my PDF is always showing up as just an icon. I want it to display the PDF since its only 1 page. The PDF seems to be fine since I can send the email and I can open it in the mail app or on my computer.
Is this not enabled by default in iOS 6.0 or later or is there something im missing?
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
// create the message body.
NSMutableString *body = [[NSMutableString alloc] init];
// add the user name to the email body.
[body appendFormat:#"Name: %#", userName];
// add the pdf to the email.
NSData *data = [NSData dataWithContentsOfFile:path];
[controller addAttachmentData:data mimeType:#"application/pdf"
fileName:#"test.pdf"
includeExtension:YES]];
// add the body string to the email body.
[controller setMessageBody:body isHTML:NO];
// show the email controller.
[self presentViewController:controller animated:YES completion:nil];
I am using PassSlot which creates a Pass on the fly that can be added to passbook. I am trying to get it downloaded to the device to allow attaching to an email. Here is what I have so far:
[PassSlot passFromTemplateWithName:#"LoveCouponCards" withValues:values pass:^(PSPass *pass) {
[PassSlot downloadPass:pass pass:^(PSPass *pass) {
PKPass *pkpass = [pass performSelector:#selector(pass)];
NSLog(#"Pass: %#", pkpass);
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObject:#"friend#example.com"];
[picker setToRecipients:toRecipients];
[picker addAttachmentData:pkpass mimeType:#"application/vnd.apple.pkpass" fileName:#"HI"];
// Fill out the email body text
NSString *emailBody = \\
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:YES completion:nil];
}];
}];
The issue is that in the addAttachment part for the email, it throws an error that NSData can't relate to PKPass basically. How can I get pass converted to NSData so I can attach it?
UPDATE:
I tried doing
NSURL *url = pkpass.passURL;
NSData *so = [NSData dataWithContentsOfURL:url];
and then putting 'so' as the addAttachment, but it attached nothing to the email.
Firstly, the passURL property of PKPass doesn't quite work the way you think. It is not a URL to the pass itself. It is a URL that opens up the Passbook app and loads up that requested pass.
You can create a PKPass with NSData, but you can't reverse that process. It sounds as if you are trying to get a pass on device, and then e-mail it. That's not allowed - if it was, people could easily copy and distribute passes around (which isn't necessarily a good thing).
If you want to e-mail a user a pass you need to do it server, rather than client side. I'm afraid that what you're trying to do isn't possible using PassKit. Sorry!
Unfortunately the PassKit library does not provide a way to get back the NSData from a PKPass.
We already provide an API call that allows you to get the raw data of a pass.
We will extend our PassSlot SDK with a method that allows you to get the NSData without having the manually call this API method.
Update
The new SDK version 0.5 is now released. You can attach the pass with the following code:
[PassSlot passFromTemplateWithName:#"LoveCouponCards" withValues:values pass:^(PSPass *pass) {
[PassSlot downloadPass:pass pass:^(PSPass *pass) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setToRecipients:#[#"friend#example.com"]];
[picker addAttachmentData:pass.data mimeType:#"application/vnd.apple.pkpass" fileName:#"LoveCouponCard.pkpass"];
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:YES completion:nil];
}];
}];
At the moment I have a NSArray of emails, and I open a view to end an email to all of these emails:
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"App Support"];
NSArray *toRecipients = [NSArray arrayWithArray:emails];
[mailer setToRecipients:toRecipients];
NSString *emailBody = #"";
[mailer setMessageBody:emailBody isHTML:NO];
// only for iPad
mailer.modalPresentationStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:mailer animated:YES];
Is there a way to send an email to this list without opening actually opening the controller (the user won't need to press on the send button and can't change the message)??
There's no way to send the message using the MFMailComposeViewController.
If you want to send an email "silently", I have previously used SKPSMTPMessage - an SMTP client that can be used on iOS to send emails without any UI.
You can set up a Gmail account specifically for the purpose of sending the messages if required.
I've created a simple demo for you. Download it here. Note that, along with the files in the SMTP folder, you will need to link to the CFNetwork.framework in your project.
I am using the MessageUI framework to send and image via e-mail after taking the photo using UIImagePickerController. When I take the photo and then invoke the mail mail message interface I get the compose window. When testing on an iPod touch (iOS 4.3.5) and iPad (iOS 5.0.1) I see the image attachment in the body of the compose window. When testing on an iPhone (4S iOS 5.0.1) the image does not appear in the compose window, but rather I see a box the size of the image attachment with an embedded small blue box with a "?" in it.
In both cases when the mail message is sent the image appears in the message received in the Mail app - iOS devices and Mac.
What can I do to fix this?
UPDATE: I've worked around this by changing:
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(imageToSend)]
to:
NSData *imageDataJPG = [NSData dataWithData:UIImageJPEGRepresentation(imageToSend, 1.0)];
I can't see in the UIKit docs that there is anything in UIImagePNGRepresentation that would not work on an iPhone ...
(Xcode 4.2.1, ARC, 5.0 SDK, Deploy target 4.3)
Here is the code for composing the message:
-(void)displayComposerSheet
{
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
[mailPicker setSubject:#"Photo"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(imageToSend)];
[mailPicker addAttachmentData:imageData mimeType:#"image/png" fileName:#"Photo"];
// Fill out the email body text.
NSString *line1 = [NSString stringWithFormat: #"I took this photo on my %#.\n", [[UIDevice currentDevice] model]];
NSString *body = [NSString stringWithFormat:#"%#", line1];
[mailPicker setMessageBody:body isHTML:NO];
// Present the mail composition interface.
[self presentModalViewController:mailPicker animated:YES];
}
The image size is restricted so if the image you are sending through is greater than a certain dimension you'll get the effect you describe above.
I had a look at my own code and saw I had
#define MAX_MAIL_DIM 1536
Which seems to be 1024 * 1.5. Sorry I can't remember how I arrived at that number but I suspect it was trial and error.
larik, your suggestion about using JPEG for the data type worked great. PNG files at this size are way too big anyway -- around 10MB. Here's the code with the JPEG NSData:
if ([MFMailComposeViewController canSendMail]) {
picker = [[MFMailComposeViewController alloc] init];
[picker setMailComposeDelegate:self];
[picker setSubject:#"My Picture"];
NSString *emailBody = #"";
[picker setMessageBody:emailBody isHTML:YES];
NSData *data = UIImageJPEGRepresentation(tempImage, 0);
[picker addAttachmentData:data mimeType:#"image/jpg" fileName:#"CameraImage"];
}