How can I append text to the bottom of my MFMailComposeViewController? - iphone

I would like to add the text "Sent from " to the bottom of the message
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:[NSString stringWithFormat:#"A message from: %#",self.profileName]];
[controller setMessageBody:[NSString stringWithFormat:#"%#",self.cellBody] isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
}
else {
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:#"E-Mail Not Enabled" message:#"E-Mail is not supported on this device" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}

You could set the message body as isHTML:YES and use line breaks <br> or try using \n in the setMessageBody string
Then just add your "sent from" message after that.

Try out this code I have changed the code as per your requirement
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:[NSString stringWithFormat:#"A message from: %#",self.profileName]];
[controller setMessageBody:[NSString stringWithFormat:#"%#\nSent from : %#",self.cellBody,self.profileName] isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
}
else {
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:#"E-Mail Not Enabled" message:#"E-Mail is not supported on this device" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
Happy Coding...

Related

Warning: Attempt to present <SLTwitterComposeViewController: 0x233141c0> on <MainController: 0x2083ebb0> whose view is not in the window hierarchy

I am receiving this error when trying to post an image to Twitter using the following code, cannot think what I am doing wrong any ideas for how to solve?;
- (void)PostToTwitter
{
UIImage* pxImage = [[self GetResultImage] retain];
TWTweetComposeViewController *twitter = [[[TWTweetComposeViewController alloc] init] autorelease];
[twitter setInitialText:#"Twitter Pic"];
[twitter addImage:pxImage];
FaceAppDelegate* app = (FaceAppDelegate*)[[UIApplication sharedApplication] delegate];
[app.mainCtrl presentViewController:twitter animated:YES completion:NULL];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res)
{
if(res == TWTweetComposeViewControllerResultDone)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Success" message:#"You have posted successfully." delegate:self cancelButtonTitle:#"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
else if(res == TWTweetComposeViewControllerResultCancelled)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Cancelled" message:#"You have cancelled posting to Twitter." delegate:NULL cancelButtonTitle:#"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
[app.mainCtrl dismissModalViewControllerAnimated:YES];
};
[pxImage release];
}
I think, that your view isn't presented. Try to use app.window.rootViewController, not property mainContr.

Using MFMailController in UITabbar controller

I am using UITabbarcontroller in one of the viewcontroller, In one tabbar i need to have only Mail controller, when i am doing so , then that is entering into infinite loop, how would i overcome it, The Code related to
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[mailViewController setToRecipients:[NSArray arrayWithObject:#"k.sourish.k#gmail.com"]];
[mailViewController setSubject:#"Subject Goes Here."];
[mailViewController setMessageBody:#"Your message goes here." isHTML:NO];
[self presentModalViewController:mailViewController animated:YES];
}
else {
NSLog(#"Device is unable to send email in its current state.");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error" message:#" Please Configure Your Mail Account" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
[mailViewController release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { NSString *message = #"";
// Notifies users about errors associated with the interface
switch (result) {
case MFMailComposeResultCancelled:
message = #"Mail: canceled";
break;
case MFMailComposeResultSaved:
message = #"Mail: saved";
break;
case MFMailComposeResultSent:
message = #"Mail: sent";
break;
case MFMailComposeResultFailed:
message = #"Mail: failed";
break;
default:
message = #"Mail: not sent";
break;
}
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[controller dismissModalViewControllerAnimated:YES];
}
This is how I would do it
Drag and drop a UITabBarButton object on your UITabBar and call it "Email". Now create this IBAction
-(IBAction)composeMyEmail
{
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[mailViewController setToRecipients:[NSArray arrayWithObject:#"k.sourish.k#gmail.com"]];
[mailViewController setSubject:#"Subject Goes Here."];
[mailViewController setMessageBody:#"Your message goes here." isHTML:NO];
[self presentModalViewController:mailViewController animated:YES];
}
else {
NSLog(#"Device is unable to send email in its current state.");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error" message:#" Please Configure Your Mail Account" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
[mailViewController release];
}
Connect your IBAction to your "Email" button.
Remove all the code you have under viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

MFMailComposeViewController model does not display

I am presenting MFMailComposeViewController in my app for Email.
But the view is not getting disply , only upper SEND and CANCEL buttons getting displayed.
This is the screen shot of view that is getting display after clicking on "Email to Subscribe" button.
I have checked out all the related codes. I have added "MessageUI" framework and imported
MessageUI/MessageUI.h , MessageUI/MFMailComposeViewController.h
I have used following code :
- (void)viewWillAppear:(BOOL)animated {
[self.view setFrame:CGRectMake(0, 62, 320, 418)];
[APPDELEGATE.window addSubview:self.view];
[self.navigationController.navigationBar setHidden:NO];
self.navigationItem.title=#"Free Subscription";
[super viewWillAppear:animated]; }
-(void)viewWillDisappear:(BOOL)animated {
[self.view removeFromSuperview];
[super viewWillDisappear:animated]; }
-(IBAction)btnEmailPressed {
MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init];
if (Apicker != nil)
{
[Apicker setSubject:#"Free Subscription"];
[Apicker setMessageBody:#" " isHTML:NO];
NSArray *toRecipients = [NSArray arrayWithObjects:#"info#xyz.com", nil];
[Apicker setToRecipients:toRecipients];
Apicker.mailComposeDelegate = self;
if([MFMailComposeViewController canSendMail])
{
[self presentModalViewController:Apicker animated:YES];
}
else
{
}
[Apicker release];
} }
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError
*)error{
NSString *message;
switch (result) {
case MFMailComposeResultCancelled:
message =#"You have canceled your email.";
break;
case MFMailComposeResultFailed:
message=#"Your email is failed";
break;
case MFMailComposeResultSent:
message=#"Your email was successfully sent.";
break;
case MFMailComposeResultSaved:
message=#" Your email has been saved";
break;
default:
message=#" Your email is not send";
break;
}
UIAlertView *alt=[[UIAlertView alloc]initWithTitle:#"" message:message delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alt show];
[alt release];
[self dismissModalViewControllerAnimated:YES];
}
I could not get the reason for this problem.
Thanks.
Its for IOS 6
-(void)email{
MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"xyz#gmail.com", nil]];
[composer setSubject:#""];
// [composer.setSubject.placeholder = [NSLocalizedString(#"This is a placeholder",)];
[composer setMessageBody:#"" isHTML:NO];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:composer animated:YES completion:nil];
}
else {
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (error) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"error" message:[NSString stringWithFormat:#"error %#",[error description]] delegate:nil cancelButtonTitle:#"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissViewControllerAnimated:YES completion:nil];
}
else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
Just try
For iOS 5
-(void)email:(id)sender{
MFMailComposeViewController *composer=[[MFMailComposeViewController alloc]init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"xyz#gmail.com", nil]];
[composer setSubject:#""];
// [composer.setSubject.placeholder = [NSLocalizedString(#"This is a placeholder",)];
[composer setMessageBody:#"" isHTML:NO];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:composer animated:YES];
}
else {
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (error) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"error" message:[NSString stringWithFormat:#"error %#",[error description]] delegate:nil cancelButtonTitle:#"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissModalViewControllerAnimated:YES];
}
else {
[self dismissModalViewControllerAnimated:YES];
}
}
try modifying your code like this :-
-(IBAction)btnEmailPressed
{
MFMailComposeViewController* Apicker = [[MFMailComposeViewController alloc] init];
if (Apicker != nil)
{
[Apicker setSubject:#"Free Subscription"];
[Apicker setMessageBody:#" " isHTML:NO];
NSArray *toRecipients = [NSArray arrayWithObjects:#"info#xyz.com", nil];
[Apicker setToRecipients:toRecipients];
Apicker.mailComposeDelegate = self;
if([MFMailComposeViewController canSendMail])
{
[self presentModalViewController:Apicker animated:YES];
}
else
{
NSString *recipients = #"mailto:info#xyz.com?cc=&subject=";
NSString *body = #"&body=";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
[Apicker release];
}
}
add these two delegate in .h file MFMessageComposeViewControllerDelegate and MFMailComposeViewControllerDelegate
UPDATE
if ([MFMailComposeViewController canSendMail]) {
appDelegate.imgCapture = [self captureView];
[appDelegate.imgCapture retain];
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
NSString *mailBody = #"Set Your Body Message";
[mailComposeViewController setMessageBody:mailBody isHTML:NO];
mailComposeViewController.mailComposeDelegate = self;
[self presentViewController:mailComposeViewController animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"e-Mail Sending Alert"
message:#"You can't send a mail"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}

MailComposeViewController - when i press button "Cancel" i don't see the panel with "Draft" ,"Save Draft" and "Cancel" buttons

I am using the MFMailComposeViewController Controller like this:
MFMailComposeViewController *picker1 = [[MFMailComposeViewController alloc] init];
picker1.mailComposeDelegate = self;
[picker1 setSubject:#"I have a pencil for you"];
UIImage *roboPic = [UIImage imageNamed:#"RobotWithPencil.jpg"];
NSData *imageData = UIImageJPEGRepresentation(roboPic, 1);
[picker addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"RobotWithPencil.jpg"];
NSString *emailBody = #"This is a cool image of a robot I found. Check it out!";
[picker1 setMessageBody:emailBody isHTML:YES];
picker1.navigationBar.barStyle = UIBarStyleBlack;
[self presentModalViewController:picker1 animated:YES];
[picker1 release];
When I press the "Cancel" button I don't see the panel with "Draft", "Save Draft" and "Cancel" buttons, the screen is locked/frozen but the panel with buttons mentioned above doesn't appear.
I will be glad to get any assistance.
Thanks in advance
Moshe
you have to use this for mail composing. and in interface file write this delegte MFMailComposeViewControllerDelegate . also import messageUi framework. its working.
#pragma mark Compose Mail
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposer
{
if(![MFMailComposeViewController canSendMail])
{
[self setUpMailAccount];
return;
}
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Strpy's Revange"];
[[picker navigationBar] setTintColor:[UIColor blackColor]];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#""];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"", #"", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#""];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Fill out the email body text
if([UIAppDelegate gameCodeFlag]==0)
{
NSString *emailBody = [NSString stringWithFormat:#"Game Score! %d",[UIAppDelegate scorePost]];
[picker setMessageBody:emailBody isHTML:NO];
}
else {
[UIAppDelegate readFriendPlist];
NSString *emailBody = [NSString stringWithFormat:#"Game Code! %#",[UIAppDelegate gameCode]];
[picker setMessageBody:emailBody isHTML:NO];
}
[self presentModalViewController:picker animated:YES];
[picker release];
}
// 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
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Sending..."
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
case MFMailComposeResultFailed:
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#"Sending Failed - Unknown Error :-("
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
}
[self dismissModalViewControllerAnimated:YES];
if([UIAppDelegate gameCodeFlag]==1)
{
[[CCDirector sharedDirector] pushScene:[StorePage scene]];
}
}

MFMailComposer - works in simulator but not in iPhone 2G

I am trying to add MFMailComposer to my iPhone app and not having much luck getting it to launch in my iPhone 2G. It always launches the email app to the accounts page and closes my app. Works like a charm in the simulator.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self displayComposerSheet];
}
-(void)displayComposerSheet
{
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// you have the MFMailComposeViewController class
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSArray *mailArr = [[NSArray alloc] initWithObjects:self.sendTo,nil];
[picker setSubject:#"Hello from iCamp America"];
[picker setToRecipients:mailArr];
NSString *emailBody = #"\n\n\email created with iCamp America\nwww.iCampAmerica.com";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
[mailArr release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"You cannot send an email !" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
The MFMailComposeViewController is only available on iPhone OS > 3.0. Are you running that version your development phone?
Silly me - I found that I had conflicting calls to send the email - I was calling BOTH the mailTo:URL and the MFMailComposer methods with the same action trigger.....