Changing views after sending an email - objective-c

I'm having trouble thinking of ways to change views after hitting "send" when in email mode. I have a main view that is a form that the user fills out, that info then populates the email. Now when I hit "send" I don't want the user to go back to the form page but a new one.
Thoughts? Thanks!

Add MFMailComposeViewControllerDelegate to your view controller interface.
then make the current view controller the delegate when initializing the ``
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
you will be notified of the status of the mail in the method
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
// message.text = #"Result: canceled";
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
{
UIAlertView *FailedAlert= [[UIAlertView alloc]initWithTitle:#"Mail could not be sent" message:nil delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[FailedAlert show];
[FailedAlert release];
break;
}
default:
NSLog(#"Hit default case in the switch");
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Here you should be able to load a new view based on your choice and result.

Related

ObjectiveC MessageComposerViewController on iPhone device. All is ok with iPad

I've got a problem with my universal app. In particular my problem is about using MessageComposerViewController (MCVC). I want to send email with a pdf attached and all works done. My problem occurs when I dismiss the MessageComposerViewController. Indeed when I remove MCVC from main view I'd like that subviews in mainView continue to has exclusiveTouch tag flagged to true.
This behavior occurs when I run my app on iPad but it doesn't happen when I use iPhone. When I use iPhone device, after I've sent an e-mail (or after I've aborted it) the touch signal is captured by mainview and I don't know the reason.
My hierarchy is:
mainView (GameView) within some subviews that I use to manage file actions: (open file, send file, remove file). To apply the right actions I've linked a UITapGestureRecognizer to the 3 buttons. After MCVC is dismissed, I'd like to continue using the three buttons but I can't.
I post some code:
MCMessageComposerViewController.h:
#interface MessageComposerViewController : UIViewController{
MFMailComposeViewController *mc;}
MCMessageViewController.m:
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
self.feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
self.feedbackMsg.text = #"Result: Mail sending canceled";
break;
case MFMailComposeResultSaved:
self.feedbackMsg.text = #"Result: Mail saved";
break;
case MFMailComposeResultSent:
self.feedbackMsg.text = #"Result: Mail sent";
break;
case MFMailComposeResultFailed:
self.feedbackMsg.text = #"Result: Mail sending failed";
break;
default:
self.feedbackMsg.text = #"Result: Mail not sent";
break;
}
[mc.navigationController popViewControllerAnimated:YES];
[mc dismissViewControllerAnimated:YES completion:nil];
[self.view removeFromSuperview];
}
- (void)displayMailComposerSheet:(NSString *)_fileName{
NSString *pathFile = #"blablablaPath";
NSString *emailTitle = "blablablaTitle";
mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
// Get the resource path and read the file using NSData
NSData *fileData;
while(!fileData){
fileData = [NSData dataWithContentsOfFile:pathFile];
if(fileData) break;
[f_t createPdfWithName:_fileName];}//create pdf file if it isn't exist
[mc addAttachmentData:fileData mimeType:#"application/pdf" fileName:_fileName];
[self presentViewController:mc animated:YES completion:NULL];}
GameView.m:
- (void)sendFile:(UITapGestureRecognizer *)recognizer{
mail = [[MessageComposerViewController alloc] init];
[self addSubview:mail.view];
[mail displayMailComposerSheet: fileName];
}
I'd like to post an image but I've not enough reputation to do this...
Please help me!!! :-)

How to setToRecipients MFMailComposerViewController in Xcode6

I am Using Xcode 6.
I am using MFMailComposerViewController to send emails within the app.
I am able to call MFMailComposerViewController,which provides the standard Interface to send emails.
I am able to set the subject as well.
But I am not able to set the Recipients.
Here is my code.
if ([MFMailComposeViewController canSendMail]) {
mailComposer = [[MFMailComposeViewController alloc]init];
[mailComposer setSubject:#"PSA Mobile CRM"];
NSArray *toRecipients = [NSArray arrayWithObject:#"abc#mass.uk.net"];
[mailComposer setToRecipients:toRecipients];
mailComposer.mailComposeDelegate = self;
[self presentViewController:mailComposer animated:YES completion:nil];
}
else {
NSLog(#"This device cannot send email");
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult (MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultSent:
NSLog(#"You sent the email.");
break;
case MFMailComposeResultSaved:
NSLog(#"You saved a draft of this email");
break;
case MFMailComposeResultCancelled:
NSLog(#"You cancelled sending this email.");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: An error occurred when trying to compose this email");
break;
default:
NSLog(#"An error occurred when trying to compose this email");
break;
}
[self dismissViewControllerAnimated:YES completion:NULL]; }
I am able to send the email.
But I am not able to set the setToRecipients method.
Can anyone please help me out with this?

Trouble sending email in iOS 7 using MFMailComposeViewController

I have a very simple app to test sending email, but the email never arrives. I have included the MessageUI framework in the app, and implemented the MFMailComposeViewControllerDelegate as well. The two methods in the app are as follows:
- (IBAction)showEmail:(id)sender
{
// Email Subject
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"iOS programming is so fun!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"email#address.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
and the delegate method:
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail sent failure: %#", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
When I press the email button in my app the first method works perfectly and when I click the send button that is presented, the "Mail Sent" message appears in the log. The email simply never arrives.
Everything seems to work as advertised, with the exception of the email never arriving at its destination.
I am running this on the iPad not in the simulator, and I have a good network connection.
What am I missing?
Well apparently this was an email configuration problem with the iPad itself. After a reboot of the device the above code works perfectly. I absolutely hate this kind of problem.
Use the canSendMail method.
Use the following code to check if a device mail configuration is setup or not. Otherwise your app will crash.
if ([MFMailComposeViewController canSendMail])
{
// Create and show the MailComposeViewController
}
else
{
// Show No mail account set up on device.
}

App crashing after touching email text MFMailComposeViewController

I'm implementMFMailComposeViewController in my demp app. However, for some reasons, my app crashes after I touch to add texts in the email text field. But sending WITHOUT adding any text works fine.
My Xcode is not showing too much. Here is what I'm getting:
I'm setting an initial text already in the email text field. Maybe the issue is here? request any code and I'll be happy to include it.
UPDATE:
Here 2 methods where the first method openMail fires up when touching the UIButton
- (IBAction)openMail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Feedback from Demo App user"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"myEMAIL#hotmail.com", #"myEMAIL2#gmail.com", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody = #"Happy to hear your feedback!";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email message was queued.");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved: you saved the email message in the drafts folder.");
break;
case MFMailComposeResultSent:
NSLog(#"Mail send: the email message is queued in the outbox. It is ready to send.");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: the email message was not saved or queued, possibly due to an error.");
break;
default:
NSLog(#"Mail not sent.");
break;
}
// Remove the mail view
[self dismissModalViewControllerAnimated:YES];
}
Probably you're using ARC, and you're not have strong pointer to your MFMailComposeViewControllerand after display, ARC is releasing it.
use
#property (nonatomic,stron) MFMailComposeViewController *mail;
and when you're initialize MFMailComposeViewController:
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
self.mail = mailer;
Do you still have this line in your code:
[mailer release];
If so that is the problem. If not, then PLEASE update our code sample so its exactly what you are using now.

ipad mail composer application to send a image to another address

i done a sample ipad mail composer application to send a image to another address.so i wrote the following code:
#import <messageUI/MFMailComposeViewController.h>
//to compose mail
-(IBAction)composeMail{
if([self validateImageView]){
[self sendSelectedImage];
}
else{
[self showAlert];
}
}
//to validate image view
-(BOOL)validateImageView{
if(selectedImageView.image){
return YES;
}
else{
return NO;
}
}
//to send selected image
-(void)sendSelectedImage{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
#try {
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello from Triassic!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"shamsudheen#triassicsolutions.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"shamsudheen#triassicsolutions.com", #"shamsudheen#triassicsolutions.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#"shamsudheen#triassicsolutions.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Attach an image to the email
NSData *myData = UIImagePNGRepresentation(selectedImageView.image);
[picker addAttachmentData:myData mimeType:#"image/jpeg" fileName:#"rainy"];
// Fill out the email body text
NSString *emailBody = #"It is raining in Trivandrum!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
#catch (NSException * ex) {
NSLog([NSString stringWithFormat:#"%#",ex]);
}
#finally {
[picker release];
}
}
//to show a alert box
-(void)showAlert{
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:#"Please select a image from PhotoAlbums!" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:#"Continue", nil];
[alertView show];
[alertView release];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation==UIInterfaceOrientationLandscapeRight || interfaceOrientation==UIInterfaceOrientationLandscapeLeft);
}
#pragma mark -
#pragma mark Dismiss Mail/SMS view controller
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the
// message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
#try {
feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
feedbackMsg.text = #"Mail sending canceled";
break;
case MFMailComposeResultSaved:
feedbackMsg.text = #"Mail saved";
break;
case MFMailComposeResultSent:
feedbackMsg.text = #"Mail sent";
break;
case MFMailComposeResultFailed:
feedbackMsg.text = #"Mail sending failed";
break;
default:
feedbackMsg.text = #"Mail not sent";
break;
}
}
#catch (NSException * ex) {
NSLog([NSString stringWithFormat:#"%#",ex]);
}
#finally {
[self dismissModalViewControllerAnimated:YES];
}
}
so when compose button clicks it will show a popup with the entered mail address and with all the details.
it showing the result send process is done successfully.but i am not getting any mail to shamsudheen#triassicsolutions.com.may i know what is the mistake i done.can i send a mail through this to another email by entering the popup through the address section.i think the compose method works when popup is loading.then how can i send a mail to address that in entered in the displayed popup.it not working fine ..may i know whats the mistake i done
In Simulator you cannot send the mail because If you want to send a mail to another person first you have to set your maildetails(you have to login to your account) in the account settings in the Device. But that feature is not exists in the Simulator.That's why you cannot send the email from Simulator.