Say I have myTextField.text = #"hello world";.
How do I save that to a .txt file and then send it in an email attachment?
Take a look at this post: Send a file as attachment in objective c
In your case, displaying the sheet would be something like:
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Check out this image!"];
// Attach the text field content to the email
UIImage *coolImage = ...;
NSData *myData = UIImagePNGRepresentation(coolImage);
NSData* data = [myTextField.text dataUsingEncoding:NSUTF8StringEncoding];
[picker addAttachmentData:data mimeType:#"text/plain" fileName:#"myText.txt"];
// Fill out the email body text
NSString *emailBody = #"My text is attached";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
More info here: https://developer.apple.com/library/ios/documentation/MessageUI/Reference/MFMailComposeViewController_class/#//apple_ref/occ/instm/MFMailComposeViewController/addAttachmentData:mimeType:fileName:
Related
I am sending SQLite(Coredata sqlite file) file via email but found empty data in all tables in the received mail.
I am using below code:
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:subject];
[mc setMessageBody:body isHTML:FALSE];
if (attachmentData) {
[mc addAttachmentData:attachmentData mimeType:fileMimeType fileName:fileName];
}
if (!recipients || recipients.count == 0) {
recipients = #[];
}
[mc setToRecipients:recipients];
[presentedViewController presentViewController:mc animated:YES completion:nil];
Here, fileMimeType = "application/x-sqlite3" and fileName: xyz.sqlite
Found the same question here, but with no solution. Any idea how to do that?
As I indicated in comments, iOS uses SQLite’s WAL mode by default, so you need to include the WAL file as an attachment.
Are you sure that you attachmentData isn't nil?
How large is your attachmentData?
How are you constructing your data from the sqlite file?
This test code seemed to work fine for me with sending a simple sample database:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self sendSampleMailWithDbAttached];
}
- (void)sendSampleMailWithDbAttached {
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:#"Message Subject"];
[mc setMessageBody:#"Message Body" isHTML:NO];
NSString *sqliteFilePath = [self createTestDb];
NSData *attachmentData = [NSData dataWithContentsOfFile:sqliteFilePath];
if (attachmentData) {
[mc addAttachmentData:attachmentData mimeType:#"application/x-sqlite3" fileName:#"xyz.sqlite"];
}
[mc setToRecipients:#[#"fake#email.com"]];
[self presentViewController:mc animated:YES completion:nil];
}
- (NSString *)createTestDb {
NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent: #"xyz.sqlite"];
sqlite3 *db;
if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {
NSString *query = #"CREATE TABLE IF NOT EXISTS test(abc TEXT, def TEXT);";
if (sqlite3_exec(db, [query UTF8String], NULL, NULL, NULL) == SQLITE_OK) {
query = #"INSERT INTO test(abc, def) VALUES('ABC', '123');";
}
sqlite3_close(db);
}
return databasePath;
}
I'm having a problem where every time I click the button that calls on my MFMessageComposeViewController nothing happens. More specifically it freezes before it displays the controller and I am not able to do anything.
Here's my code:
- (IBAction)getQuote:(id)sender {
NSString * inputProduct =_productTextField.text;
NSString * inputYear = _yearTextField.text;
NSString * inputMake = _makeTextField.text;
NSString * inputEmail = email.text;
NSString * inputDescript = _descript.text;
emailConfirmation.text = email.text;
//email subject
NSString *emailTitle = #"Love Shack Electronics";
//email content
if ([conditionlabel isEqual: #"Fair"]) {
NSString *emailBody = [NSString stringWithFormat:#"Product:%# Make:%# Year Manufactured:%# Description:%# Condition:Fair Email:%#",inputProduct,inputMake,inputYear,inputDescript, inputEmail];
NSArray *recipient = [NSArray arrayWithObject:#"LoveShackElectronics#gmail.com"];
MFMailComposeViewController *SuperLovedEmail = [[MFMailComposeViewController alloc]init];
[SuperLovedEmail setTitle:emailTitle];
[SuperLovedEmail setToRecipients:recipient];
[SuperLovedEmail setMessageBody:emailBody isHTML:NO];
[SuperLovedEmail setUserActivity:false];
[self presentViewController:SuperLovedEmail animated:YES completion:nil];
[SuperLovedEmail setEditing:false];
If anyone can help or figure out what is wrong with my code it would be greatly appreciated.
Please check in device and latest Xcode version. it's work fine. it not work then put into dispatch method like below
dispatch_async(dispatch_get_main_queue(),
^{
if ([conditionlabel isEqual: #"Fair"])
{
NSString *emailBody = [NSString stringWithFormat:#"Product:%# Make:%# Year Manufactured:%# Description:%# Condition:Fair Email:%#",inputProduct,inputMake,inputYear,inputDescript, inputEmail];
NSArray *recipient = [NSArray arrayWithObject:#"LoveShackElectronics#gmail.com"];
MFMailComposeViewController *SuperLovedEmail = [[MFMailComposeViewController alloc]init];
[SuperLovedEmail setTitle:emailTitle];
[SuperLovedEmail setToRecipients:recipient];
[SuperLovedEmail setMessageBody:emailBody isHTML:NO];
[SuperLovedEmail setUserActivity:false];
[self presentViewController:SuperLovedEmail animated:YES completion:nil];
[SuperLovedEmail setEditing:false];
}
});
I have this code on Objective-C:
- (IBAction)Mail:(id)sender {
NSString *MailTitle = #"Mi Opinion About this App";
NSString *MailBody = #"Hi, I am an user of this app and I think that ";
NSArray *MailTo = [NSArray arrayWithObject:#"contact#domain.com"];
MFMailComposeViewController *MailController = [[MFMailComposeViewController alloc] init];
MailController.mailComposeDelegate = self;
[MailController setSubject:MailTitle];
[MailController setMessageBody:MailBody isHTML:NO];
[MailController setToRecipients:MailTo];
[self presentViewController:MailController animated:YES completion:NULL];
}
It shows properly the subject, but the fields "To", and "Body" are empty in iOS simulator!
I want to present a MFMailComposeViewController from a modal view controller. Basically this method works, but it does not work reliably:
-(void)sendMailTapped:(id)sender{
[self resetButtonsStateAfterTapping:sender];
[self dismissPopover];
if (filesize>10) {
[self showAlertForExceededMaximumAttachmentSize];
return;
}
#try {
MFMailComposeViewController *picker =
[[MFMailComposeViewController alloc] init];
if ([MFMailComposeViewController canSendMail]) {
picker.mailComposeDelegate = self;
NSURL *path =[NSURL urlWithPath:[pageInfoDict valueForKey:#"file_name"]
docId:[pageInfoDict valueForKey:#"id_doc"]
encrypted:[[pageInfoDict valueForKey:#"encrypted"]
boolValue]] ;
NSString *fileName = [pageInfoDict valueForKey:#"title"];
if([fileName length] == 0) {
fileName = [path lastPathComponent];
}
if(![fileName hasSuffix:[path pathExtension]]){
fileName=[fileName stringByAppendingFormat:#".%#",[path pathExtension]];
}
[picker setSubject:[#"Send document: " stringByAppendingString:fileName]];
NSArray *ccRecipients = [NSArray arrayWithObjects:
[[CustomisationConfig getAppConfig] getCCMail],nil];
[picker setCcRecipients:ccRecipients];
NSArray *bccRecipients = [NSArray arrayWithObjects:
[[CustomisationConfig getAppConfig] getBCCMail],nil];
[picker setBccRecipients:bccRecipients];
NSData *myData = [path decryptedData];
[picker addAttachmentData:myData
mimeType:fileMIMEType(fileName) fileName:fileName];
NSString *emailBody = [NSString stringWithFormat:
#"\n\nThis file was sent using %#.",
[DCConfiguration getHumanReadableAppName] ];
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:true completion:^(void){}];
}
}
#catch (NSException *exception) {
NSLog(#"ContextMenuViewController sendMailTapped:%#",exception.description);
}
}
If I restart my iPad and open the app, the picker will be presented only on the second tap on the corresponding button.
If I click again on the button after this, the picker will be presented on first touch everytime and it works perfectly, until I shutdown and restart the iPad.
The following is printed to the console:
Warning: Attempt to present <MFMailComposeViewController: 0x200cbb70> on <ContextMenuViewController: 0x200cd1a0> whose view is not in the window hierarchy!
What is calling the sendMailTapped: method? If its interface builder, you need to change it to IBAction instead of void, and connect them in interface builder.
I need one button called email button to do one selection from multiple item from picker view to attach that selected item via email. I am stuck at the IBAction. Here is my progress.
M File :
-(void)pickerViewEmail:(UIPickerView *)pickerViewEmail didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([[musicList objectAtIndex:row] isEqual:#"m1"])
{
MFMailComposeViewController *pickerEmail = [[MFMailComposeViewController alloc] init];
pickerEmail.mailComposeDelegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:#"m1" ofType:#"mp3"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[pickerEmail addAttachmentData:myData mimeType:#"audio/mp3" fileName:#"m1"];
[pickerEmail setSubject:#"Hello!"];
// 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"];
[pickerEmail setToRecipients:toRecipients];
[pickerEmail setCcRecipients:ccRecipients];
[pickerEmail setBccRecipients:bccRecipients];
// Fill out the email body text
NSString *emailBody = #"Hello";
[pickerEmail setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:pickerEmail animated:YES];
[pickerEmail release];
}
if ([[musicList objectAtIndex:row] isEqual:#"m2"])
{
}
if ([[musicList objectAtIndex:row] isEqual:#"m3"])
{
}
IBAction :
-(IBAction)showEmail
{
if ([MFMailComposeViewController canSendMail])
{
[self pickerEmail]; I have a yellow error when i call this. What is the right solution?
}
else
{
}
}
iOS : How to attach a multiple attachment file in one button using pickerview method?
I don't understand your call [self pickerEmail]
Higher in your code, pickerEmail seems to be an object, of type MFMailComposeViewController, not a method. So the call [self pickerEmail] does not have any sense in Objective-C