iPhone 短信功能

4
我在我的应用程序中使用以下代码来创建和填充短信,以便用户发送。通过按下UIButton调用此代码。
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
    if([MFMessageComposeViewController canSendText])
    {
        controller.body = @"Hello from Mugunth";
        controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];
        controller.messageComposeDelegate = self;
        [self presentModalViewController:controller animated:YES];
    }

在视图的初始加载时,我想测试设备的功能,并在必要时隐藏按钮(例如在iPod touch上)。
有没有关于如何做到这一点的代码示例?请注意,我只针对iOS 4.0及更高版本,我知道上述代码将无法在使用早期版本iOS的设备上运行。

[MFMessageComposeViewController canSendText] 能够满足我需要的功能吗?我没有 iPod touch 设备来进行测试。 - Mick Walker
3个回答

4

[MFMessageComposeViewController canSendText]将确定您所在的设备是否可以发送文本消息。我已经知道它在带有SIM卡的iPhone、iPod Touch和iOS模拟器上能够正确地反应。我没有在没有SIM卡的iPhone或iPad上进行过测试。


2
你可以使用以下代码来检测设备是否能够发送短信:

推荐使用以下代码:

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"sms:"]]) {

}

或者
[MFMessageComposeViewController canSendText]

0
请确保在分配MFMessageComposeViewController实例之前调用[MFMessageComposeViewController canSendText]。较新版本的iOS会在您实例化MFMessageComposeViewController时立即向用户发出警报,告知他们无法发送消息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接