Sending a message or email from iOS devices
Often you need your application to be able to send users emails or messages. In iOS, this can be done simply by MessageUI framework.
At the end of the article you can download the whole example. Now we assume that we know the email address or phone number of the user to whom the message we send.
The interface might look like this:
Adding Message Framework into sources
The first step you need to do is add the necessary frameworks to the project. In this case the only need MessageUI.framework
We’ll do it as well as the following figure
Creating a header file
Header files will be in this case include two protocols, that declared methods which can be Implemented.
Protokols:
- MFMailComposeViewControllerDelegate
- MFMessageComposeViewControllerDelegate
Interface in a header file might look like the following code
1 2 3 |
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate> @end |
Sending Email
If you have prepared all the necessary variables, we can simply create a new composer.
1 2 3 4 5 6 7 8 9 10 11 |
MFMailComposeViewController* mc = [[MFMailComposeViewController alloc] init]; //set delegate mc.mailComposeDelegate = self; //set message body [mc setMessageBody:messageBody isHTML:NO]; //set message subject [mc setSubject:messageSubject]; //set message recipients [mc setToRecipients:[NSArray arrayWithObject:email]]; |
In this case we assume that we have a ready variables: messageBody (message text), messageSubject (message subject) and email (email address to recipient).
Finally, open the dialog window for sending email.
1 |
[self presentViewController:mc animated:YES completion:nil]; |
Now we have to check what has happened with our email, and whether it was sent. Write the method mailComposeController, which contains information about the result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ //if result is possible if(result == MFMailComposeResultSent || result == MFMailComposeResultSaved || result == MFMailComposeResultCancelled){ //test result and show alert switch (result) { case MFMailComposeResultCancelled: [self makeAlert:@"Result Cancelled"]; break; case MFMailComposeResultSaved: [self makeAlert:@"Result saved"]; break; //message was sent case MFMailComposeResultSent: [self makeAlert:@"Result Sent"]; break; case MFMailComposeResultFailed: [self makeAlert:@"Result Failed"]; break; default: break; } } //else exists error else if(error != nil){ //show error [self makeAlert:[error localizedDescription]]; } //dismiss view [self dismissViewControllerAnimated:YES completion:nil]; } |
Examples include a method makeAlert, here you will find the prime example (here)
Sending message
Sending messages is similar to that sending emails, but with the difference that we use MFMessageComposeViewController.
The main difference is that the device may not be able to send messages, this we can simply verify with the following code:
1 2 3 4 5 |
if([MFMessageComposeViewController canSendText] ){ //device is possible to send messages }else{ //device can't send messages } |
In the case that you can send messages, you can prepare MFMessageComposeViewController:
1 2 3 4 5 6 |
MFMessageComposeViewController* comp = [[MFMessageComposeViewController alloc] init]; //set properties comp.body = messageBody; comp.recipients = [NSArray arrayWithObject:phone]; comp.messageComposeDelegate = self; |
In this case we assume that we have a ready variables: messageBody (message text) and phone (phone number)
Finally, open the dialog for sending messages:
1 |
[self presentViewController:comp animated:YES completion:nil]; |
The last thing is that we must check whether the message was sent or not. We create function MessageComposeViewController that contains the result.
It may look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ //test result switch (result) { case MessageComposeResultCancelled: [self makeAlert:@"Result canceled"]; break; //message was sent case MessageComposeResultSent: [self makeAlert:@"Result sent"]; break; case MessageComposeResultFailed: [self makeAlert:@"Result Failed"]; break; default: break; } //dismiss view [self dismissViewControllerAnimated:YES completion:nil]; } |
Important is call DismissViewControllerAnimated. The application will return back to our view.
Complete example
Complete example can be found on github. Also contains a simple interface to enter a phone number and email.
Nice Article.