Monday, 28 September 2015

How to work with Mail and Sms In Ios

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>



@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate >

@property (weak, nonatomic) IBOutlet UIButton *btnSendMail;
@property (weak, nonatomic) IBOutlet UIButton *btnSendSMS;

- (IBAction)btnSendMailClick:(id)sender;
- (IBAction)btnSendSMSClick:(id)sender;

- (IBAction)btnSendSMSOutSideAppClick:(id)sender;

@end



-----------------------------------------------------------------------------------
- (IBAction)btnSendMailClick:(id)sender {
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
       
        mail.mailComposeDelegate = (id)self;
        [mail setSubject:@"Your Mail Subject"];
        [mail setMessageBody:@"Here is some main text in the email!" isHTML:NO];
        [mail setToRecipients:@[@"dhanesh.gosai@indianic.com",@"test@gmail.com"]];
       
        //For Attachment File In mail.
        //Media Type http://www.iana.org/assignments/media-types/media-types.txt
        NSData *fileData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pdf-sample" ofType:@"pdf"]];
        [mail addAttachmentData:fileData mimeType:@"application/pdf" fileName:@"MySample.pdf"];
       
        [self presentViewController:mail animated:YES completion:NULL];
    }
    else
    {
        NSLog(@"This device cannot send email");
    }
}

#pragma mark MFMailComposeViewControllerDelegate methods

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    switch (result) {
        case MFMailComposeResultSent:
            NSLog(@"You sent the email.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"You saved a draft of this email");
            break;
        case MFMailComposeResultCancelled:
            NSLog(@"You cancelled sending this email.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed:  An error occurred when trying to compose this email");
            break;
        default:
            NSLog(@"An error occurred when trying to compose this email");
            break;
    }
   
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (IBAction)btnSendSMSClick:(id)sender {
//    InSide the app
    if(![MFMessageComposeViewController canSendText]) {
        UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [warningAlert show];
        return;
    }
   
    NSArray *recipents = @[@"12345678", @"72345524"];
    NSString *message = [NSString stringWithFormat:@"Just Check Message form My APP"];
   
    MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
    messageController.messageComposeDelegate = self;
    [messageController setRecipients:recipents];
    [messageController setBody:message];
   
    // Present message view controller on screen
    [self presentViewController:messageController animated:YES completion:nil];

}

- (IBAction)btnSendSMSOutSideAppClick:(id)sender {
//    Out Side App
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:9408003812"]];
   
}

#pragma mark MFMessageComposeViewController methods

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    switch (result) {
        case MessageComposeResultSent:
            NSLog(@"You sent the message.");
            break;
        case MessageComposeResultFailed:
            NSLog(@"Message failed:  An error occurred when trying to compose this message");
            break;
        case MessageComposeResultCancelled:
            NSLog(@"You cancelled sending this message.");
            break;
        default:
            NSLog(@"An error occurred when trying to compose this email");
            break;
    }
   
    [self dismissViewControllerAnimated:YES completion:NULL];
}