Send a File as Attachment in Objective C

Send a file as attachment in objective c

Use the method below

-(void)displayComposerSheet 
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Check out this image!"];

// Set up recipients
// NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
// NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];

// [picker setToRecipients:toRecipients];
// [picker setCcRecipients:ccRecipients];
// [picker setBccRecipients:bccRecipients];

// Attach an image to the email
UIImage *coolImage = ...;
NSData *myData = UIImagePNGRepresentation(coolImage);
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"coolImage.png"];

// Fill out the email body text
NSString *emailBody = @"My cool image is attached";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];

[picker release];
}

And implement the delegate method

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(@"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(@"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Result: failed");
break;
default:
NSLog(@"Result: not sent");
break;
}
[self dismissModalViewControllerAnimated:YES];
}

And in your interface file

#import <MessageUI/MFMailComposeViewController.h>
...

@interface ... : ... <MFMailComposeViewControllerDelegate>

How to send email with attachment(file PDF) in app iOS?

Below it's correct sollution:

 - (IBAction)showEmail:(id)sender {

NSString *emailTitle = @"elllo";

NSString *messageBody = @"Hi ! \n Below I send you ";

NSArray *toRecipents = [NSArray arrayWithObject:@"m1891@gmail.com"];

NSString *path = [[NSBundle mainBundle] pathForResource:@"MV" ofType:@"pdf"]; NSData *myData
= [NSData dataWithContentsOfFile: path];

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];

mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc addAttachmentData:myData mimeType:@"application/pdf" fileName:@"MV.pdf"];

[mc setToRecipients:toRecipents];

[self presentViewController:mc animated:YES completion:NULL];

}

Sending a email with pdf attachment from documents directory Objective c

plz use this code

- (IBAction)sendMailWithAttachedFile
{

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForResourse:fileName ofType:extension]];


//Get the file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.pdf"];

NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:path];
NSData *data=[[NSData alloc]initWithContentsOfURL:outputURL];
[picker addAttachmentData:data mimeType:@"application/pdf" fileName:@"data.pdf"];
[self presentModalViewController:picker animated:YES];


}

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissViewControllerAnimated:YES completion:nil];
}

Can I send UIDocuments by email, as an attachment?

You could do like the following.

Create a MFMailComposeViewController and use - (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename method to add your attachment.

For example.

MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[mailVC setSubject:@"Shared documents"];
[mailVC setToRecipients:@[@"sample@example.com"]];
[mailVC setMessageBody:@"Here the docs I want to share" isHTML:NO];
[mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"file.pdf"];

[mailVC setMailComposeDelegate:self];

[self presentViewController:mailVC animated:YES completion:nil];

where pdfData is of type NSData. So, you need to transform your document into a NSData.

From Apple doc.

addAttachmentData:mimeType:fileName:

This method attaches the specified data after the message body but
before the user’s signature. You may attach multiple files (using
different file names) but must do so prior to displaying the mail
composition interface. Do not call this method after presenting the
interface to the user.

About the second part of your question. Could you explain what type of document do you need to display?

In the meantime, take a look at Adding "Open In..." option to iOS app.

how to attach any document in email in iphone app

Try following code snippet.

- (NSString *)pathForFile : (NSString *) fileName{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: fileName];
}

- (void)sendMailWithAttachedFile:(NSString *) fileName extention:(NSString *) extension{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForResourse:fileName ofType:extension]];
NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForFile:[NSString stringWithFormat:@"%@.%@", fileName, extension]]];
NSData *data=[[NSData alloc]initWithContentsOfURL:outputURL];
[picker addAttachmentData:data mimeType:@"application/pdf" fileName:@"TestOne.pdf"];
[self presentViewController:picker animated:YES completion:nil];
}

Now Call Send Mail method As:

[self sendMailWithAttachedFile:@"TestOne" :@"pdf"];

Objective C - How does Calendar adds file attachments to events?

There is no single answer to your question. It depends on the server (Fruux, iCloud, Exchange, Yahoo, ...), client version etc.

There is a simple answer to this: Can I attach files using the EventKit API? No, you can't. EventKit functionality is very limited. The 'notes' property maps to the (first or last? ...) iCalendar description property.

For iCloud and OSX server the OSX/iOS client should do CalDAV managed attachments. For servers not supporting this, the client might inline the attachments in iCalendar attachments. But if I remember right there are also cases in which the client keeps the attachment just local and refers to it via a file: URL in the ATTACH property.

Summary: If you want to attach files to iCloud (or other CalDAV servers), you need to implement a CalDAV client.



Related Topics



Leave a reply



Submit