I am using MFMailComposeViewController in my app and the mail sending part seems to be OK.
But when I leave the mail app, things go wrong :
- one toolbar (UIToolbar object) has disappeared.
- one pointer (UIImageView*) has become nil, without me doing anything for that to happen.
In other words the calling environment is changed although I do not want it to change.
Where could be my mistake?
Here is my code, in case someone can see something wrong :
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error
{
[controller dismissModalViewControllerAnimated:YES];
}
-(IBAction)sendAsEMail {
MFMailComposeViewController *mailComposeViewController=[[MFMailComposeViewController alloc] init];
mailComposeViewController.mailComposeDelegate=self;
[mailComposeViewController setSubject:#"Mail subject"];
[mailComposeViewController setMessageBody:#"This is for you !" isHTML:NO];
[mailComposeViewController addAttachmentData:
[NSData dataWithContentsOfFile:[[My_ViewController getDocDir] stringByAppendingPathComponent:
[pictureNames objectAtIndex:userItemSelected]]]
mimeType:#"image/png" fileName:#"Picture.png"];
if (mailComposeViewController) [self presentModalViewController:mailComposeViewController animated:YES];
[mailComposeViewController release];
}
Thanks for any piece of relevant information.
Try out this link it explain in detail
Add framework
Then .h file header files
Then .m file the mail code
Check at this link.
Related
As you can see in this , the keyboard is going up too high when I touch the textarea. Any idea on what's causing this particular problem. How can I make the keyboard stick to the bottom of screen ?
[UPDATE] (code):
#import "ViewController.h"
#import <MessageUI/MessageUI.h>
#interface SettingsViewController () <MFMailComposeViewControllerDelegate>
#end
#implementation ViewController
- (IBAction)feedBack:(id)sender {
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
NSString *model = [[UIDevice currentDevice] model];
NSString *version = #"1.0";
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:[NSArray arrayWithObjects: #"feedback#example.com",nil]];
[mailComposer setSubject:[NSString stringWithFormat: #"Feedback about App V%#",version]];
NSString *supportText = [NSString stringWithFormat:#"Device: %#\niOS Version:%#\n\n",model,iOSVersion];
supportText = [supportText stringByAppendingString: #"Please write your feedback or suggestions"];
[mailComposer setMessageBody:supportText isHTML:NO];
[self presentViewController:mailComposer animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult: (MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
That has nothing to do with your code. The user can move the keyboard from the bottom of the screen or split it. You have done exactly that as the user. You, as the user, need to drag the keyboard down again if that's what you want.
And you don't do anything about it, because it is behaviour that the user intentionally chose. For people who write code that keeps track of the keyboard appearing / disappearing and moving items out of the area covered by the keyboard: If the keyboard is undocked or split, or if the user uses a hardware keyboard, you will get no notifications about the area covered by the keyboard.
My App got rejected and reason is below:-
Did not integrate with iOS features. For example, the email button should enable users to compose emails in the app rather than launching the Mail app.
I did not get that what they want. I have used MFMailComposer class so what's wrong with it?Any Suggestion.
Did you do it like this:
- (IBAction)pushMail:(id)sender { //A button that initiates composition
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"My Mail Subject"];
if (controller) [self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;
{
if (result == MFMailComposeResultSent) {
NSLog(#"It's away!");
}
[self dismissModalViewControllerAnimated:YES];
}
I think you have to use an MSMailComposeViewController (as I have in the above example) to do what you want.
... the email button should enable users to compose emails in the app ...
They mean that your program should allow people to compose emails, instead of opening Mail.app.
I created an app based on the "Tabster" sample code from Apple. The app runs great, but I want to add email to it. I created an email app. and have tried every approach I can think of, learn from a tutorial, or read, but it continually crashes. I posed the question on the Apple Dev. forum and several responses were to simply "copy the files over to the existing app. and you should be good." Obviously its not this simple. I have added the MessageUI Framework and have tried copying the files in many different ways and Im am still stuck. Its Friday and this one problem has held me up since Monday. I guess the first part is the fact that there are 2 main.m files and I have tried combining them, I have tried renaming the mail's main.m file to emailmain.m. I dont know what else to try.
Its amazing to me that all of the documentation and all of the tutorials out there about creating email within an iOS app all start off with creating a new application. What am I missing? How do I add email into a fully functioning app. I would appreciate any guidance, links to literature, or tutorials on the subject.
Any help I can get on this will be tremendously appreciated. There are several other types of things I would like to add on to it, but I cant even get email implemented into it!
Thank you for help you can provide.
John
This is the method I use all the time. It should be able to be added to ANY UIViewController simple and cleanly.
Import your framework:
#import <MessageUI/MessageUI.h>
Make sure you include your delegate in the interface:
#interface MyViewController : UIViewController <MFMailComposeViewControllerDelegate>
Then in your implementation add this method or a variation if you need it to be an IBAction or something like that:
- (void)sendEmail
{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *email = [[MFMailComposeViewController alloc] init];
email.mailComposeDelegate = self;
[email setSubject:#"My Email Subject"];
[email setMessageBody:#"My message body." isHTML:NO];
[self presentModalViewController:email animated:YES];
} else {
UIAlertView *emailAlert = [[UIAlertView alloc] initWithTitle:#"Email Failure" message:#"Your device is not configured to send email" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[emailAlert show];
}
}
You can call this method on button click or anything you want. It will pull up an email composer view where your user can hit send.
You need to import the MessageUI Framework. Where ever you want to use it import it in the corresponding .h file and set up the MFMailComposeViewControllerDelegate.
#import <MessageUI/MessageUI.h>
#interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>
Then when you want to send a message use the following code in the .m file:
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
// Optional Configuration Parameters to make life easier for the user
[mailViewController setSubject:subjectString];
[mailViewController setMessageBody:messageString isHTML:YES];
[mailViewController setToRecipients:recipientsArray];
// Present the VIew Controller and clean up after ourselves
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
Add the appropriate delegate method, you can use it to dismiss the controller once the email is sent:
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissModalViewControllerAnimated:YES];
}
Here Is asample code to create email with an image as attachment .you can modify it according to your needs.
-(void)createEmail
{
NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:#"<html><body>"] retain];
[emailBody appendString:#"<p>Some email body text can go here</p>"];
UIImage *emailImage = [UIImage imageNamed:#"myImageName.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
NSString *base64String = [imageData base64EncodedString];
[emailBody appendString:[NSString stringWithFormat:#"<p><b><img src='data:image/png;base64,%#'></b></p>",base64String]];
[emailBody appendString:#"</body></html>"];
NSLog(#"%#",emailBody);
//mail composer window
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
[emailDialog setSubject:#"My Inline Image Document"];
[emailDialog setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
[emailBody release];
}
I would like to add to my application a "Tell Friend" option which allow user to select multiple contacts to send them email. Contact need to be filtered to the one who have email address only.
does any one know such ready example that I could reuse.
I recently searching for the same problem and I found iTellAfriend. It works for me.
Download this source code from github/iTellafriend. Open zip file and inside src file drag iTellAFriend.h and iTellAFriend.m to your project. Check "Copy items into destination group folder(if needed)" and "Create group folder for any added folders"
In your appdelegate.m add #import "iTellAFriend.h"
Add following to your appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//[iTellAFriend sharedInstance].appStoreID = yourAppId;
[iTellAFriend sharedInstance].appStoreID = 408981381; //example
return YES;
}
Add #import "iTellAFriend.h" to your ViewController.m and anywhere in your ViewController.m call following method (preferably in a button)
if ([[iTellAFriend sharedInstance] canTellAFriend]) {
UINavigationController* tellAFriendController = [[iTellAFriend sharedInstance] tellAFriendController];
[self presentModalViewController:tellAFriendController animated:YES];
}
In iTellAFriend.m modify following
- (UINavigationController *)tellAFriendController
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:self.messageTitle];
[picker setMessageBody:[self messageBody] isHTML:YES];
return picker;
}
to
- (UINavigationController *)tellAFriendController
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObjects:#"xxxx#xxxx.com", #"xxxxx#xxxx.com", nil];
[picker setToRecipients:toRecipients];
[picker setSubject:self.messageTitle];
[picker setMessageBody:[self messageBody] isHTML:YES];
return picker;
}
when you click your button following scene will appear it wont send the email on simulator but on device
I have a webview object (aWebView) which was added on top of current window like this -
UIWindow *webWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 20, 320,460)];
[webWindow addSubview:aWebView];
[webWindow makeKeyAndVisible];
I have a ViewController (viewcontrollerobj) which is subView of aWebView -
[webView addSubview:viewcontrollerobj.view];
Then I am calling sendInAppMail method in the ViewController-
[sviewcontroller sendInAppMail];
SendInAppMail looks like this -
MFMailComposeViewController *mailController = [[[MFMailComposeViewController alloc] init] autorelease];
if([MFMailComposeViewController canSendMail])
{
[mailController setMessageBody:#"hello" isHTML:NO];
[mailController setSubject:#"subject"];
mailController.mailComposeDelegate = self;
[self presentModalViewController:mailController animated:YES];
[mailController release];
}
didFinishWithResult looks like this -
- (void)mailComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSent:
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Mail viewcontroller shows up fine. The problem is that when I hit cancel it shows the delete/save draf t option and after clicking either delete/save the mail viewcontroller doesn't go away!
When I look at console it shows this log message -
"Presenting action sheet clipped by its superview. Some controls might not respond to touches. On iPhone try -[UIActionSheet showFromTabBar:] or -[UIActionSheet showFromToolbar:] instead of -[UIActionSheet showInView:]."
I am not using UIActionSheet anywhere and haven't used in the past so I am not able to understand what it is saying.
I looked at this - https://stackoverflow.com/a/6015957/516938
But it seems like the solution given is very specific to a situation.
Not sure this is the issue, but this is the first thing I would look at.
Based on the error message that you got it sounds like either one of the views (the aWebView or the one from viewcontrollerobj that you defined) doesn't allow enough space for the MFMailComposeViewController, meaning that the dimensions of it are smaller than the MFMailComposeViewController requires. It isn't actually clipping the content, so you see it, but it is blocking the touches so that they don't get to the MFMailComposeViewController.
I hope that is clear enough - I had a hard time describing my thoughts here correctly.