短信选项无法正常工作

时间:2011-11-14 16:58:45

标签: iphone objective-c ios sms

我正在使用xcode 4.2,我正在开发iphone APP,这个应用程序的一部分正在发送短信

这是我输入的代码:

-(IBAction)SMSbutton{
    MFMessageComposeViewController *sms = [[MFMessageComposeViewController alloc] init];
        if ([MFMessageComposeViewController canSendText]) {
            sms.body= [NSString stringWithFormat: @"text"];
            sms.messageComposeDelegate = self;
            [self presentModalViewController:sms animated:YES];
}

我可以打开短信应用程序并发送短信,但问题是我是否点击“取消”或“发送”按钮,消息应用程序不会消失。怎么做?

感谢

3 个答案:

答案 0 :(得分:3)

将您的调用类设置为消息编写器的委托,然后捕获取消并发送事件以根据需要关闭模式视图控制器。

设置头文件以符合MFMessageComposeViewControllerDelegate,初始化编辑器时,将消息委托设置为self:

MFMessageComposeViewController *george = [MFMessageComposeViewController alloc] init];
george.messageComposeDelegate = self;

然后实现委托方法......最简单的方法是:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{   
    [self dismissModalViewControllerAnimated:YES];
}

...但您可以根据需要捕捉并处理错误和事件。

答案 1 :(得分:1)

在您的代理“

中实施messageComposeViewController:didFinishWithResult:
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
      if(result == MessageComposeResultCancelled) {
           //Message cancelled
      } else if(result == MessageComposeResultSent) {
           //Message sent
      }

      [self dismissModalViewControllerAnimated:YES];
}

答案 2 :(得分:0)

您是否尝试过dismissViewControllerAnimated:completion:dismissModalViewControllerAnimated:

[self dismissModalViewControllerAnimated: YES];