Xcoder
Xcoder

Reputation: 51

iOS trigger Email and SMS in single action

Firstly appreciate your help with regard to below query.

I am developing a iPhone app where requirement is that user will click a Button and application should bring up Email panel with default test and once user decides to "send" or "cancel", application should bring up SMS panel for user to action either "Send" or "Cancel"

Application does slide in eMail window with default text and once user presses "Send" or "Cancel" program flow back to Method "didFinishWithResult" but later SMS Window is not displayed. When I comment send of Mail and directly jump to SMS application does display SMS window.

I believe it is because when application request for eMail it starts different thread and later when thread returns back to didFinishWithResult method sending SMS is not working. I am not sure how to solve this problem..

Please help

- (void) mailComposeController:(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;
}
[self showSMSPicker]; <<<<<====== call for SMS
[self dismissModalViewControllerAnimated:YES]; 

after which Code correctly flows to method [self showSMSPicker]. In showSMSPicker method

-(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];
                   smsComposer.messageComposeDelegate = self;
                   [self presentModalViewController:smsComposer animated:YES];
              }
               [smsComposer release];
    }
    else {  
            NSLog( @"Device not configured to send SMS.");

    }
}
else {
    NSLog(@"Device not configured to send SMS.");
}
}

Upvotes: 1

Views: 920

Answers (3)

ahbuddha
ahbuddha

Reputation: 11

I believe that the animation is causing the display of the SMS modal to conflict. Also dismissModalViewController is deprecated so use:

[self dismissViewControllerAnimated:YES completion:^ {
        [self sendSMS];
}]; 

Upvotes: 1

Xcoder
Xcoder

Reputation: 51

I used a tweak method to get this accomplished, created a boolean if set to true then in ViewDIDAppear method call showSMS method

  - (void) viewDidAppear:(BOOL)animated
{
   if (self.isSMSRequired) {
      [self showSMSPicker];
       self.isSMSRequired = FALSE;
    }
}

and in

- (void) mailComposeController:(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;
}
self.isSMSRequired = TRUE;
[self dismissModalViewControllerAnimated:YES]; 

This worked...

Upvotes: 0

Kevin
Kevin

Reputation: 3131

Try dismissing the Mail Modal before presenting the SMS Message modal.

- (void) mailComposeController:(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;
}
[self dismissModalViewControllerAnimated:YES]; 
[self showSMSPicker];

Upvotes: 0

Related Questions