消息从iPhone发送到另一部手机

时间:2012-06-21 09:28:31

标签: iphone ios

在我的应用程序中使用 MFMessageComposeViewController 发送消息。 以下代码是我用来发送消息的例子

        MFMessageComposeViewController *msgController = [[[MFMessageComposeViewController alloc] init] autorelease];
        if([MFMessageComposeViewController canSendText])
        {
            msgController.messageComposeDelegate = self;
            msgController.body = [NSString stringWithFormat:@"%@", appDelegate.finalConactStr ];
            [self presentModalViewController:msgController animated:YES];
        }

并使用以下代码检查结果

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result 
{
    switch (result)
    {
    case MessageComposeResultCancelled:
        cancelAlert = [[UIAlertView alloc] initWithTitle:@"SMS a Contact" message:@"Cancelled"delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [cancelAlert show];
        [cancelAlert release];
        NSLog(@"Result: canceled");
        break;
    case MessageComposeResultSent:
        successAlert = [[UIAlertView alloc]initWithTitle:@"SMS a Contact" message:@"Successfully sent" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [successAlert show];
        [successAlert release];
        NSLog(@"Result: sent");
        break;
    case MessageComposeResultFailed:
        failAlert = [[UIAlertView alloc]initWithTitle:@"SMS a Contact" message:@"Failed" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [failAlert show];
        [failAlert release];

        NSLog(@"Result: failed");
        break;
    default:
        notSentAlert = [[UIAlertView alloc]initWithTitle:@"SMS a Contact" message:@"Not Sent" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [notSentAlert show];
        [notSentAlert release];

        NSLog(@"Result: not sent");
        break;
    }
    [self dismissModalViewControllerAnimated:YES];
 }

但是没有SIM卡也会显示成功发送

等警告

我们如何检查设备是否有SIM卡或是否有信息发送功能。 任何人都可以帮助或建议我。

提前致谢。

1 个答案:

答案 0 :(得分:0)

自Apple推出iMessage以来,所有可运行iOS5的设备都可以通过MFMessageComposeViewController发送消息。

因此,没有必要检查设备中是否安装了SIM卡,只需信任[MFMessageComposeViewController canSendText]即可告诉您设备是否可以发送消息。

  • MessageComposeResultCancelled:用户取消了合成。
  • MessageComposeResultSent:用户已成功排队或发送消息。
  • MessageComposeResultFailed:用户尝试保存或发送邮件失败。