Why MailController doesn't show To and Body text? - ios

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!

Related

Found empty data in sqlite when send via MFMailcomposeController

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;
}

MFMessageComposeViewController freezes before displaying the view controller

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];
}
});

How to export UITextField or UITextView's text to email?

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:

Email issue iOS8 in Simulator

I have an issue in iOS8 issue in Simulator in won't work ? can anybody help me for this ?
here message is String Message but it not appear in Mail sheet ?
Here's what i have tried !!
NSMutableString* sub = [NSMutableString string];
[message appendFormat:#"\n"];
[sub appendString:#"Item Id : "];
[sub appendString:item.item_id];
UIImage *img = self.thumbnailImageView.image;
NSMutableArray *actItems = [[NSMutableArray alloc] init];
[actItems addObject:message];
[actItems addObject:sub];
if(img != nil)
{
[actItems addObject:img];
}
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:actItems
applicationActivities:nil];
activityViewController.excludedActivityTypes = #[UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter,
UIActivityTypeAirDrop];
[self.navigationController presentViewController:activityViewController
animated:YES
completion:^{}];
[activityViewController setValue:sub forKey:#"subject"];
activityViewController.popoverPresentationController.sourceView = parentView;

MFMailComposeViewController only presented after second tap

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.

Resources