Monday, August 13, 2012

Sending E-mail from your iOS App

Hi Guys , In this section i will  describe you how to send an e-mail from your app. I don't think we need more explanation on this topic, as Apple has given us a simple framework <MessageUI> for doing this. You just need to know how to implement its class and delegates.
Below are the steps that will help you to send an email from your app :

1. Include MessageUI Framework to your project and import  #import "MessageUI/MessageUI.h"
2. Include  MessageUI delegate  <MFMailComposeViewControllerDelegate>       
3. Lets define some custom method in .h file for customizing the email composing view
    -(void)displayComposerSheet : (NSString*) address withSubject:(NSString*) subject withContent:(NSString*) content;
   - (void) email: (NSString*) address withSubject:(NSString*) subject withContent:(NSString*) content;
4. Now, lets make a call from where you want to display a  email composer sheet
     -(IBAction)emailMe { // This is the method from which you want to send email
[self email:@"jwarchansameer1@googlemail.com" withSubject:@"Message Header" withContent:@"your message goes here..."]; 
}



5. Here are our custom method definition :
-->
- (void) email: (NSString*) address withSubject:(NSString*) subject withContent:(NSString*) content {
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
 if (mailClass != nil){ // checking whether this class/feature is available or not in running device
     // We must always check whether the current device is configured for sending emails or not
        if ([mailClass canSendMail]  {  
     // Yes, proceed your email composition
   [self displayComposerSheet:address withSubject:subject withContent:content];
        }
        else{
      // No, display information of failure
   UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error !" message:@"Sorry, this device is not configured to send email.\n Kindly configure your email account in Device Settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
    else
    {   // No Support of this feature
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error !" message:@"Sorry, This Feature is only available in iPhone OS 3.0 or later." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }  
}

-->
// Present  email composition interface inside your app. It populates all the Mail fields.
-(void)displayComposerSheet : (NSString*) address withSubject:(NSString*) subject withContent:(NSString*) content{
      MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
      picker.mailComposeDelegate = self;
     [picker setSubject:   subject]; // your message title/header
     NSArray *toRecipients = [NSArray arrayWithObject:address];
    [picker setToRecipients:toRecipients]; // Receipient's email addresses

    //    ——- Attaching any File to mail ——- // Optional
  /*  NSString *filePath = [[NSBundle mainBundle] pathForResource:@”Smile” ofType:@”png”];
       NSData *myData = [NSData dataWithContentsOfFile:filePath];
       [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"Smile.png"];*/
   
    //    ——- set cc mail address ——-  // Optional
 ///   [picker setCcRecipients:toRecipients];
    //    ——- set Bcc mail address ——-  // Optional
  ////  [picker setBccRecipients:toRecipients];
   
     [picker setMessageBody:content isHTML:YES]; // this sets your message
     //picker.modalPresentationStyle = UIModalPresentationFormSheet;
    // picker.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    // picker.navigationItem.title =@"Test";
    [self presentModalViewController:picker animated:YES];
} 
 6. Finally, we will use the receiving delegate method. In this delegate method, we will track whether email is sent or not, or cancelled by user .
-->
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {  
    // Notifies users about errors associated with the interface
          switch (result) {
        case MFMailComposeResultCancelled:
               NSLog(@"Email canceled by user.");
               break;
        case MFMailComposeResultSaved:
               NSLog(@"Email draft saved.") ;
         break;
        case MFMailComposeResultSent:
               NSLog(@"Email sent.");
               break;
     case MFMailComposeResultFailed:
               NSLog(@"Email failed.");
               break;
        default:
               NSLog(@"Email not sent.");
            break;
    }
 [self dismissModalViewControllerAnimated:YES];  
}

No comments:

Post a Comment