Wednesday, 20 July 2016

How to send message in iOS Programatically

Today we will learn how to send message programmatically in iOS using MessageUI framework.

1. Create a new Xcode project (lets say MessageDemo).
2. Add MessageUI framework.

4. Add one button in Storyboard, your project will look like this.

5. import MessageUI framework and write the following code in your ViewController.m

#import "ViewController.h"
#import <MessageUI/MessageUI.h>

@interface ViewController ()<MFMessageComposeViewControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
    switch (result) {
        case MessageComposeResultCancelled:
            break;
            
        case MessageComposeResultFailed:
        {
            
            UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Failed !" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *defaultAction  = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil];
            [controller addAction:defaultAction];
            [self presentViewController:controller animated:YES completion:nil];
            break;
        }
            
        case MessageComposeResultSent:
            break;
            
        default:
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)sendMessage:(id)sender{
    
    if(![MFMessageComposeViewController canSendText]) {
        UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Device not supported !" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *defaultAction  = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil];
        [controller addAction:defaultAction];
        [self presentViewController:controller animated:YES completion:nil];
        return;
    }
    
    NSArray *recipents = @[@"767676767"];
    NSString *message = @"Check iostpoint.wordpress.com";
    
    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];
}
6. Now run the code.. Run on device because simulator will not support this.

Download the whole Xcode project from here.
Please share and give your valuable feedback if you like it.

Enjoy coding :)

No comments:

Post a Comment

Ads Inside Post

Blogger Widget