Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 6th, 2012  |  syntax: None  |  size: 2.93 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to send mail in xcode using ARC
  2. #import <UIKit/UIKit.h>
  3. #import <QuartzCore/QuartzCore.h>
  4. #import <MessageUI/MFMailComposeViewController.h>
  5.  
  6. @interface secondViewController : UIViewController <MFMailComposeViewControllerDelegate> {
  7.  
  8.     IBOutlet UILabel *resultLabel;
  9. }
  10. -(IBAction)switchToFirst:(id)sender;
  11. -(IBAction)openMail:(id)sender;
  12.  
  13. @end
  14.        
  15. #import "secondViewController.h"
  16. #import "ViewController.h"
  17.  
  18. @interface secondViewController ()
  19.  
  20. @end
  21.  
  22. @implementation secondViewController
  23.  
  24.  
  25. -(IBAction)openMail:(id)sender {
  26.  
  27.     MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
  28.     [composer setMailComposeDelegate:self];
  29.     if ([MFMailComposeViewController canSendMail]) {
  30.         [composer setToRecipients:[NSArray arrayWithObjects:@"", nil]];
  31.         [composer setSubject:@""];
  32.         [composer setMessageBody:@"message" isHTML:YES];
  33.         [composer setModalTransitionStyle:UIModalTransitionStylePartialCurl];
  34.         [self presentModalViewController:composer animated:YES];
  35.     }
  36.     else {
  37.  
  38.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Can't send your email!" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
  39.         [alert show];
  40.  
  41.     }
  42.  
  43. }
  44.  
  45. -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
  46.     switch (result) {
  47.         case MFMailComposeResultSent:
  48.             resultLabel.text = @"Mail was sent";
  49.             break;
  50.  
  51.         case MFMailComposeResultSaved:
  52.             resultLabel.text = @"Mail was saved to drafts";
  53.             break;
  54.  
  55.         case MFMailComposeResultFailed:
  56.             resultLabel.text = @"Mail wasn't able to sent";
  57.             break;
  58.  
  59.         case MFMailComposeResultCancelled:
  60.             resultLabel.text = @"Mail was Cancelled";
  61.             break;
  62.  
  63.  
  64.     }
  65.  
  66.     [self dismissModalViewControllerAnimated:YES];
  67. }
  68.  
  69.  
  70. -(IBAction)switchToFirst:(id)sender {
  71.     ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
  72.     [self presentModalViewController:second animated:YES];
  73.  
  74. }
  75.  
  76. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  77. {
  78.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  79.     if (self) {
  80.         // Custom initialization
  81.     }
  82.     return self;
  83. }
  84.  
  85. - (void)viewDidLoad
  86. {
  87.     [super viewDidLoad];
  88.     // Do any additional setup after loading the view from its nib.
  89.     // Set the background image by setting the contents of the view's layer
  90.     UIImage *bg = [UIImage imageNamed:@"iPad iDeaf Assisstant Background.png"];
  91.     self.view.layer.contents = (id) [bg CGImage];
  92.  
  93. }
  94.  
  95. - (void)viewDidUnload
  96. {
  97.     [super viewDidUnload];
  98.     // Release any retained subviews of the main view.
  99.     // e.g. self.myOutlet = nil;
  100. }
  101.  
  102. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  103. {
  104.     return (interfaceOrientation == UIInterfaceOrientationPortrait);
  105. }
  106. @end