- How to send mail in xcode using ARC
- #import <UIKit/UIKit.h>
- #import <QuartzCore/QuartzCore.h>
- #import <MessageUI/MFMailComposeViewController.h>
- @interface secondViewController : UIViewController <MFMailComposeViewControllerDelegate> {
- IBOutlet UILabel *resultLabel;
- }
- -(IBAction)switchToFirst:(id)sender;
- -(IBAction)openMail:(id)sender;
- @end
- #import "secondViewController.h"
- #import "ViewController.h"
- @interface secondViewController ()
- @end
- @implementation secondViewController
- -(IBAction)openMail:(id)sender {
- MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
- [composer setMailComposeDelegate:self];
- if ([MFMailComposeViewController canSendMail]) {
- [composer setToRecipients:[NSArray arrayWithObjects:@"", nil]];
- [composer setSubject:@""];
- [composer setMessageBody:@"message" isHTML:YES];
- [composer setModalTransitionStyle:UIModalTransitionStylePartialCurl];
- [self presentModalViewController:composer animated:YES];
- }
- else {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Can't send your email!" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
- [alert show];
- }
- }
- -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
- switch (result) {
- case MFMailComposeResultSent:
- resultLabel.text = @"Mail was sent";
- break;
- case MFMailComposeResultSaved:
- resultLabel.text = @"Mail was saved to drafts";
- break;
- case MFMailComposeResultFailed:
- resultLabel.text = @"Mail wasn't able to sent";
- break;
- case MFMailComposeResultCancelled:
- resultLabel.text = @"Mail was Cancelled";
- break;
- }
- [self dismissModalViewControllerAnimated:YES];
- }
- -(IBAction)switchToFirst:(id)sender {
- ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
- [self presentModalViewController:second animated:YES];
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- // Set the background image by setting the contents of the view's layer
- UIImage *bg = [UIImage imageNamed:@"iPad iDeaf Assisstant Background.png"];
- self.view.layer.contents = (id) [bg CGImage];
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- @end