iOS以单一操作触发电子邮件和短信

时间:2012-02-24 18:04:18

标签: iphone ios email sms

首先感谢您对以下查询的帮助。

我正在开发一个iPhone应用程序,其中要求用户将点击一个按钮,应用程序应该调出带有默认测试的电子邮件面板,一旦用户决定“发送”或“取消”,应用程序应该为用户调出SMS面板动作“发送”或“取消”

应用程序会在默认文本的eMail窗口中滑动,一旦用户按下“发送”或“取消”程序流回到方法“didFinishWithResult”,但稍后不会显示SMS窗口。当我评论发送邮件并直接跳转到短信应用程序时显示短信窗口。

我相信这是因为当eMail的应用程序请求启动不同的线程时以及稍后当线程返回到didFinishWithResult方法时发送SMS不起作用。我不知道如何解决这个问题..

请帮忙

- (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]; 

之后Code正确地流向方法[self showSMSPicker]。在showSMSPicker方法

-(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.");
}
}

3 个答案:

答案 0 :(得分:1)

我认为动画导致SMS模式的显示发生冲突。同时不推荐使用dismissModalViewController,因此请使用:

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

答案 1 :(得分:0)

在显示SMS消息模式之前尝试解除邮件模态。

- (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];

答案 2 :(得分:0)

我使用了一个tweak方法来完成这个,如果设置为true则创建一个布尔值然后在ViewDIDAppear方法中调用showSMS方法

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

并在

- (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]; 

这有效......