I am using Apple's sample code for the MessageUI and MFMailComposeViewControllerDelegate, and mostly it works great. But for some reason when I implement it, the text in the recipient fields appears out of alignment with the field labels, and you can only see half of the cursor and half of the text while typing. Once you've typed the addresses and exited the field, the text is completely visible, though still out of alignment with the labels. I have looked at other apps' implementations of the MessageUI, and they do not seem to have this problem. has anyone seen this problem and found a solution?
Below is code:
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Data"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"email#example.com"];
[picker setToRecipients:toRecipients];
// Attach an attachment to the email
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *csvFile = [documentsDirectory stringByAppendingPathComponent:#"myFile.csv"];
NSData *myData = [NSData dataWithContentsOfFile:csvFile];
NSString *filename = #"myFile.csv";
[picker addAttachmentData:myData mimeType:#"text/csv" fileName:filename];
// Fill out the email body text
NSString *emailBody = #"Attached is the data";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
The problem occurs in both the simulator and on device.
Probably, you've got a custom UITextField in your app and have some custom code in - (CGRect)textRectForBounds:(CGRect)bounds. If you'd like to have some category for UITextField class, try to put your specific code directly to the class where you using that instead of using category, which affects all textfields in your app, even in MFMailComposeViewController
Related
So I have had this problem for sometime and just cant get it working! I have been building a survey app that users simply enter information in and its saved to a csv file. Im now at the stage where I need to attached the csv file within the app to an email address...
I have the code as follows, it should work but for some reason is attaching a 'blank' csv file instead of the one with the data in. Im guessing it must be something to do with the file path however cant get it working!!
- (IBAction)send:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedFilePath = [documentsDirectory stringByAppendingPathComponent:#"results.csv"];
NSData *csvData = [NSData dataWithContentsOfFile:savedFilePath];
MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc] init];
[mailcomposer addAttachmentData:csvData mimeType:#"text/csv" fileName:#"results.csv"];
[mailcomposer setToRecipients:#[#"gpsflighttrial#gcap.eu"]];
[mailcomposer setSubject:self.subject.text];
[mailcomposer setMessageBody:self.message.text isHTML:NO];
}
UPDATE:
So I just tested this on my new i-phone and there is no attachment when the email is delivered? Its there in the mail app and in the simulator, however when the message is received the attachment has gone? Can anyone help??
I've just tried your code and it seems ok. Add this code to see what you actually have in that csv file.
NSString *strData = [[NSString alloc]initWithData:csvData encoding:NSUTF8StringEncoding];
NSLog(#"Output %#",strData);
Try to check csvData contents, it's probably empty.
When use my following script, then the picture and the logfile would be a attached file. Also some other Text (the default signature) from the email would be attached as index.htm to the email when i open it with Outlook.
The Email is sent out from the iPad with iOS 7.0.6
In the Mail-Application on the Mac, the Files are in the middle of the text, before the signature of the mail-application would be written.
When i attach only the image, then all would be fine. When i attache also the logfile.txt, then its like I have descripted and in the Mail-Application of the mac, all special keys (german like ä, ü) are not decoded as utf8.
Where did I make the mistake?
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
currentIOS = [[UIDevice currentDevice] systemVersion];
vNumber = #"V"VersionNumber;
subject = [NSString stringWithFormat:#"%# - Meine aktuelle iPad-Nummer",vNumber];
message = [NSString stringWithFormat:#"<p>Hallo liebes EDV-Team,</p> <p>mein Betriebssystem ist: %#</p><p>Und meine iPad-Nummer lautet: %#</p>", currentIOS, udid];
[controller setToRecipients: [NSArray arrayWithObjects:#"email#webpage.de", nil]];
[controller setSubject:subject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *txtFilePath = [documentsDirectory stringByAppendingPathComponent:#"logfile.txt"];
NSData *noteData = [NSData dataWithContentsOfFile:txtFilePath];
[controller addAttachmentData:noteData mimeType:#"text/plain" fileName:#"logfile.txt"];
UIImage *roboPic = [UIImage imageNamed:#"world_smilie.png"];
NSData *imageData = UIImagePNGRepresentation(roboPic);
[controller addAttachmentData:imageData mimeType:#"image/png" fileName:#"smile.png"];
[controller setMessageBody:message isHTML:YES];
if ([self respondsToSelector:#selector(presentViewController:animated:completion:)]){
[self presentViewController:controller animated:YES completion:nil];
}
I changed the code for the textfile:
I change the code like elio.d told me:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *txtFilePath = [documentsDirectory stringByAppendingPathComponent:#"logfile.txt"];
NSString *txtFileContent = [NSString stringWithContentsOfFile:txtFilePath encoding:NSUTF8StringEncoding error:nil];
NSData *txtFileData = [txtFileContent dataUsingEncoding:NSUTF8StringEncoding];
[controller addAttachmentData:txtFileData
mimeType:#"text/plain"
fileName:#"logfile.txt"];
The logfile.txt would be attached but the signature from the ipad would be attached to the email in ATT00002.htm and now i got also a empty ATT00001.htm
I have the same problem when i change the type of the email (isHTML:NO)
To save user time when emailing, user saves there email address in a preferences page that should pre fill the recipient when email is composed. (or thats what im trying to do) Here is where im stuck, how do I use my saved string to an object for the purpose of pre filling the recipient.
Currently the string does not pre fill the recipient
saves here in preferences page:
NSString *savecontents = _emailAddress.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savecontents forKey:#"saveEmail"];
[defaults synchronize];
Reads here in mail view presentation
- (IBAction)email:(id)sender {
NSString *savedValue = [[NSUserDefaults standardUserDefaults] <---------- saved email string
stringForKey:#"saveEmail"];
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObject:savedValue]; <------- trying to get string here
[mail setToRecipients:toRecipients];
[mail setSubject:#"subject"];
NSString *emailBody = [NSString stringWithFormat: #"text here"] ;
[mail setMessageBody:emailBody isHTML:YES];
mail.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController:mail animated:YES];
}
tried so far:
NSArray *toRecipients = [NSString stringWithFormat:savedValue];
[mail setToRecipients:toRecipients];
and
NSArray *toRecipients = [NSString stringWithFormat:#"%#",savedValue];
[mail setToRecipients:toRecipients];
and
Google, SO and banging fist on desk
All you need is this:
if (savedValue.length) {
[mail setToRecipients:#[ savedValue ]];
}
This uses modern Objective-C array syntax. This would be the same as:
if (savedValue.length) {
NSArray *toRecipients = [NSArray arrayWithObject:saveValue];
[mail setToRecipients:toRecipients];
}
The code you have you are trying to assign an NSString value to an NSArray variable.
Also, please don't use stringWithFormat unless you actually have a need to format a string.
Hopefully the title made sense. Here's my situation.
I have one PDF created in Pages then exported as a PDF. I then create another PDF within the app. I create the PDF file with this code:
- (void)makePDF:(NSString*)fileName :(NSDictionary*)dictResults
{
pageSize = CGSizeMake(612, 792);
_strPDFJustCreated = fileName;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[self generatePdfWithFilePath:pdfFileName:dictResults];
}
- (void)generatePdfWithFilePath:(NSString *)thefilePath :(NSDictionary*)dictResults
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
BOOL done = NO;
do {
...whole lot of coding goodness
done = YES;
} while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
Both files are attached to the email message with this code:
/**********Attach PDF Files**************/
//get path to pdf file
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* pdfFilePath = [documentsDirectory stringByAppendingPathComponent:strPDFFileName];
//convert pdf file to NSData
NSData* pdfData = [NSData dataWithContentsOfFile:pdfFilePath];
//attach the pdf file
[mailViewController addAttachmentData:pdfData mimeType:#"application/pdf" fileName:strPDFFileName];
//get path to pdf file
NSString* pdf2FilePath = [documentsDirectory stringByAppendingPathComponent:#"OER_MarketingContent.pdf"];
//convert pdf file to NSData
NSData* pdf2Data = [NSData dataWithContentsOfFile:pdf2FilePath];
//attach the pdf file
[mailViewController addAttachmentData:pdf2Data mimeType:#"application/pdf" fileName:#"OER_MarketingContent.pdf"];
[self presentViewController:mailViewController animated:YES completion:NULL];
When I take a peak inside the app folder within the iPhone Simulator folder the pdf files are there, they are recognized by the OS as PDF files and I can open and read them. However, when I get an email message delivered (and don't laugh but we use Lotus Notes), I cannot preview the files I can't an error message saying there is no filter available, when I opt to open it (in Preview) it just hangs.
I use the same code in other apps with no problems. My guess then is that I am doing something different somewhere in my code. I can't see anything obvious so my question would be is there a way to test say NSData for success/failure prior to attaching the file or testing it post attachment but pre email?
Thanks
Don't use UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);. Instead, use: NSData * pdfData = [NSData dataWithContentsOfFile:pdfFileName];.
So, the whole thing should end up looking like:
NSString *fileName = #"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[self generatePdfWithFilePath:pdfFileName];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setToRecipients:[NSArray arrayWithObject:#"email#email.com"]];
[controller setSubject:#"PDFTEST"];
[controller setMessageBody:#"Your text here" isHTML:NO];
NSData * pdfData = [NSData dataWithContentsOfFile:pdfFileName];
NSString *testFileName = [NSString stringWithFormat:#"PDFNAME"];
[controller addAttachmentData:pdfData mimeType:#"application/pdf" fileName:testFileName];
[self presentModalViewController:controller animated:YES];
This worked for me. Let me know if you have any more questions or if this is unclear. Thanks!
I write the code to open the pdf file located
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *pdfFilePath =[documentsDir stringByAppendingPathComponent:#"421_core_animation_essentials.pdf"];
fileURL = [NSURL URLWithString:pdfFilePath];
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
[[self navigationController] pushViewController:previewController animated:YES];
[previewController.navigationItem setRightBarButtonItem:nil];
[previewController release];
It is working while testing in simulator but when testing in iPad device it doesn't work.
please help me.
iOS device file systems are case-sensitive unlike the simulator which isn't.
Therefore check the filename for case sensitivity on:
NSString *pdfFilePath =[documentsDir stringByAppendingPathComponent:#"421_core_animation_essentials.pdf"];