Introduction to iOS Triggering Email and SMS in Single Action
In this article, we will explore the process of triggering both email and SMS messages from an iPhone application. We will delve into the inner workings of the MFMailComposeViewController
and MFMessageComposeViewController
classes, which handle email and SMS composition respectively.
Understanding iOS Messaging Frameworks
The iOS messaging frameworks provide a standardized way for applications to send emails and SMS messages. The MFMailComposeViewController
class is used to compose and send emails, while the MFMessageComposeViewController
class is used to compose and send SMS messages.
When an application wants to trigger an email or SMS message, it must create an instance of either MFMailComposeViewController
or MFMessageComposeViewController
, depending on the type of message it wishes to send. The user can then be presented with a modal view controller that allows them to enter the desired message and recipient(s).
However, there is often a requirement for the application to trigger both email and SMS messages in a single action. This can be achieved by using a combination of the MFMailComposeViewController
and MFMessageComposeViewController
classes.
Creating the App’s Messaging Flow
To create an app that triggers both email and SMS messages, we need to follow these steps:
- Create an instance of either
MFMailComposeViewController
orMFMessageComposeViewController
, depending on the type of message you want to send. - Set up the view controller with the necessary information, such as the recipient(s) and message body.
- Present the view controller modally.
However, when we present a modal view controller for email composition, it can sometimes interfere with other views in our app, including those that are used to display SMS messages. This is because the animation used to slide in the email view controller can conflict with the layout of other views in our app.
Solving the Issue: Animation and Dismiss
To solve this issue, we need to modify our code to handle the dismiss of the modal view controller in a way that respects the current state of our app. The animation used to slide in the email view controller can conflict with the layout of other views in our app.
The dismissModalViewControllerAnimated
method is deprecated and should not be used. Instead, we should use the dismissViewControllerAnimated
method or implement a completion block using the completion
parameter.
Here’s how you might modify your code to handle this:
## Modifying the Code
### Step 1: Create an Instance of MFMailComposeViewController
```markdown
- (void)mailController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Result: Mail sending canceled");
break;
case MFMailComposeResultSaved:
NSLog(@"Result: Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Result: Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Result: Mail sending failed");
break;
default:
NSLog(@"Result: Mail not sent");
break;
}
// Call for SMS
[self showSMSPicker];
// Dismiss the email view controller
[self dismissViewControllerAnimated:YES completion:^{
[self sendSMS]; // Send SMS after dismissing email view controller
}];
}
Step 2: Create an Instance of MFMessageComposeViewController
- (void)showSMSPicker {
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass != nil)
{
// Check whether the current device is configured for sending SMS messages
if ([messageClass canSendText])
{
MFMessageComposeViewController *smsComposer = [[MFMessageComposeViewController alloc] init];
smsComposer.messageComposeDelegate = self;
if([MFMessageComposeViewController canSendText]) {
smsComposer.body = @"Sending SMS";
smsComposer.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];
// Present the email view controller and dismiss it after sending an SMS
[self dismissViewControllerAnimated:YES completion:^{
[self sendSMS]; // Send SMS after dismissing email view controller
}];
[smsComposer release];
}
}
}
else {
NSLog( @"Device not configured to send SMS.");
}
}
Conclusion
In this article, we explored the process of triggering both email and SMS messages from an iPhone application. We discussed how to use the MFMailComposeViewController
and MFMessageComposeViewController
classes to compose and send emails and SMS messages respectively.
We also examined a common issue that can occur when presenting a modal view controller for email composition, such as conflicting with other views in our app.
Finally, we showed you how to modify your code to handle the dismiss of the modal view controller in a way that respects the current state of your app. This should help resolve any issues related to animation and dismissing the email view controller.
Last modified on 2024-09-03