i am a newbie to iOS and am trying to add inapt email in my app. I have a screen in which pushing the email icon should open the inapp email. I have the code for the inapp email. However, the button is already an outlet on the controller. So, I don't know how to link the same button to a different class/file which has the code for the inapp email. I was thinking of setting up a delegate but don't know how to initialize the delegate in the mail class. Have been struggling for a few days...please help!
Sumit
Try MFMailComposeViewController.... here is some sample code:
Make sure you import the MEssageUI framework and import the MFMailComposeViewController/MessageUI in .h and also conform to its delegate
MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
[mailView setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[mailView setSubject:#"Interesting Apple News Article!"];
NSString *mailString = [[NSString alloc] initWithFormat:#"Test!"];
[mailView setMessageBody:mailString isHTML:NO];
[mailView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:mailView animated:YES];
[mailString release];
[mailView release];
} else
[mailView release];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mailing Error" message:[error localizedDescription] delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self dismissModalViewControllerAnimated:YES];
} else {
[self dismissModalViewControllerAnimated:YES];
}
}
Related
- (IBAction)showEmail:(id)sender {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController* composeVC = [[MFMailComposeViewController alloc] init];
composeVC.mailComposeDelegate = self;
// Configure the fields of the interface.
[composeVC setToRecipients:#[#"address#example.com"]];
[composeVC setSubject:#"Hello!"];
[composeVC setMessageBody:#"Hello from California!" isHTML:NO];
// Present the view controller modally.
[self presentViewController:composeVC animated:YES completion:nil];
// composeVC.modalPresentationStyle = UIModalPresentationPageSheet ;
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
}
It always goes in the else condition even when tested on real device. please help. this works in other Xcode versions
I want to get email clients into an Action and when I click on an email client the email should send from that particular client. Can anyone tell me how to implement it?
Please click on this for image
It should look like above image.
Hope this will helps you.
UIActionSheet* sheet = [[[UIActionSheet alloc] init] autorelease];
sheet.title = #"Add Accounts";
sheet.delegate = self;
[sheet addButtonWithTitle:#"iCloud"];
[sheet addButtonWithTitle:#"Google"];
[sheet addButtonWithTitle:#"Yahoo"];
if (condition)
[sheet addButtonWithTitle:#"XYZ"];
sheet.cancelButtonIndex = [sheet addButtonWithTitle:#"Cancel"];
then you have to create method for itemclick from actionsheet , you have to open mail app using following code
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController * emailController = [[MFMailComposeViewController alloc] init];
emailController.mailComposeDelegate = self;
[emailController setSubject:subject];
[emailController setMessageBody:mailBody isHTML:YES];
[emailController setToRecipients:recipients];
[self presentViewController:emailController animated:YES completion:nil];
[emailController release];
}
// Show error if no mail account is active
else {
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"You must have a mail account in order to send an email" delegate:nil cancelButtonTitle:NSLocalizedString(#"OK", #"OK") otherButtonTitles:nil];
[alertView show];
[alertView release];
}
Note, if you want to send the email from within your app itself, you can use the MFMailComposeViewController.
Apple doesnt provide an API to fetch the clients. You will have to design the ActionSheet icons on your own, and in the MFMailComposeViewController on tap of "From" field, you will have to auto populate the field with the suitable email ID.
I have this code who send a e-mail to users, Im using MFMailCompose:
.h file
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface TestViewController : UIViewController <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
#property(nonatomic,assign) id<MFMailComposeViewControllerDelegate> mailComposeDelegate;
.m file
#synthesize mailComposeDelegate
-(void)sendEmail:(NSString*)valor{
//Valor receive the email
NSString *corpoMensagem = #"No have body yet...";
// From within your active view controller
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
mailCont.mailComposeDelegate = self;// Required to invoke mailComposeController when send
[mailCont setSubject:#"FazerBem - Recibo de Compra"];
[mailCont setToRecipients:[NSArray arrayWithObject:valor]];
[mailCont setMessageBody:corpoMensagem isHTML:YES];
[self presentViewController:mailCont animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
if (error){
NSString *errorTitle = #"Erro to send";
NSString *errorDescription = [error localizedDescription];
UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:errorTitle message:errorDescription delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[errorView show];
}
[self becomeFirstResponder];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
}
when I click the send button he can send the email to the recipient and can close the screen, now when I click the cancel button, it opens 2 more options 'delete draft' and 'save draft' when I click on one of the app crashes and returns me the following error:
[TestViewController respondsToSelector:]: message sent to deallocated instance 0x16b3b470
How I can solve this problem?
Use Switch case to for performing actions:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
UIAlertView *alert;
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
alert = [[UIAlertView alloc] initWithTitle:#"Draft Saved" message:#"Composed Mail is saved in draft." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
break;
case MFMailComposeResultSent:
alert = [[UIAlertView alloc] initWithTitle:#"Success" message:#"You have successfully referred your friends." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
break;
case MFMailComposeResultFailed:
alert = [[UIAlertView alloc] initWithTitle:#"Failed" message:#"Sorry! Failed to send." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:nil];
}
Remove this line:
[self becomeFirstResponder];
That should fix your problem.
If above solution didn't work for you as it was for me, here, instead of self use instance of MFMailComposeViewController:
Instead Of
[self dismissViewControllerAnimated:YES completion:nil];
Use
[mailComposer dismissViewControllerAnimated:YES completion:nil];
I have seen a nice feature in the iOS App Scanner Pro. This app allows to send scanned documents as email attachments via the original mail app from Apple but without leaving the Scanner Pro app. I ask me how did they do it? Is there a special API call?
implement MFMailComposeViewControllerDelegate like this:
#interface YourViewController<MFMailComposeViewControllerDelegate >
Then where you want to instantiate this email viewcontroller just do the following:
if([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
[mailController setMailComposeDelegate:self];
[mailController setSubject:#"Mail Subject!"];
[mailController setMessageBody:#"Here is your message body" isHTML:NO];
[mailController setToRecipients:[NSArray arrayWithObject:#"yourrecipent#domain.com"]];
NSData *imageData = UIImageJPEGRepresentation(imageToUpload, 1.0f);
if(imageData.length)
{
[mailController addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"Your_Photo.jpg"];
[self presentModalViewController:mailController animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Invalid Image" message:#"The image couldn't be converted." delegate:nil cancelButtonTitle:nil otherButtonTitles:#"Okay", nil];
[alert show];
}
}
Last implement mailComposerViewController delegate method
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
// or you can check for the status first and implement different task if you wish
}
You can use UIActivityViewController, for example:
UIImage *image = [UIImage imageNamed:#"image_file_name"];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[image] applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
it gives user even more options, than just send email.
Yes, the so called UIActivityViewController. You use it like this:
NSArray *itemsToShare = #[[NSString stringWithFormat:#"This is a string that is sent via mail as well."], NSURLtoTheFileToSend];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact]; // Here you can say what you dont want to give the opportunity to share.
activityVC.completionHandler = ^(NSString *activityType, BOOL completed) {
if (completed) {
UIAlertView *alert = [[UIAlertView alloc] init];
alert.title = #"Export successfull";
[alert show];
[alert performSelector:#selector(dismissWithClickedButtonIndex:animated:) withObject:nil afterDelay:1];
}
};
[self presentViewController:activityVC animated:YES completion:^{}];
I want to put something like order same packet and if I finish my order I want send email; how can I modify this code to change change the body of the email?
inappemailViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface inappemailViewController :
UIViewController <MFMailComposeViewControllerDelegate> {
}
-(IBAction)email;
#end
inappemailViewController.m
-(IBAction)email {
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"123#abc.com", nil]];
[composer setSubject:#"subject here"];
[composer setMessageBody:#"message here" isHTML:NO];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:composer animated:YES];
[composer release];
}
else
[composer release];
}
-(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];
[alert release];
[self dismissModalViewControllerAnimated:YES];
}
else {
[self dismissModalViewControllerAnimated:YES];
}
}
The message body is set on this line:
[composer setMessageBody:#"message here" isHTML:NO];
So just change "message here" to whatever you want the email to say. You could also make it be an NSString object that you set elsewhere.
You can fix this code. Your message body is set on this line:
[composer setMessageBody:#"message here" isHTML:NO];
Hope this helps.