2014-11-24 96 views
0

我想检测是否iOS设备可以接收短信。从第一次看,我可以使用下面的代码:检测设备是否可以接收短信

+(bool)canDeviceSendSms { 
// Check if the device can send sms 
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"sms://"]]) { 
    // Device supports sms, lets confirm it can place one right now 
    CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init]; 
    CTCarrier *carrier = [netInfo subscriberCellularProvider]; 
    NSString *mnc = [carrier mobileNetworkCode]; 
    if (([mnc length] == 0) || ([mnc isEqualToString:@"65535"])) { 
     // Device cannot send sms at this time. SIM might be removed. 
     return NO; 
    } else { 
     // Device can send sms 
     return YES; 
    } 
} else { 
    // Device does not support sms sending 
    return NO; 
} 

}

然而,我的iPad 4的SIM卡返回我是的,当它本身是不能接收短信。它有iMessage,但例如我没有授权iMessage中的任何帐户,所以可能我不会收到任何短信。我如何检测短信接收能力并排除iMessage。

+0

返回YES有当u可以检查其中的iPhone或iPad – 2014-11-24 15:23:34

回答

2

也许您可以尝试检测iPad是否也可以发送短信,因此如果未配置iMessage,它应返回。从MFMessageComposeViewController使用方法canSendText

检查此:https://developer.apple.com/library/ios/Documentation/MessageUI/Reference/MFMessageComposeViewController_class/index.html#//apple_ref/occ/clm/MFMessageComposeViewController/canSendText

之前呈现消息组成视图,调用canSendText 类方法,以确保用户的设备适当地配置 。不要试图呈现如果 的canSendText方法返回NO消息编写图。如果没有的iMessage,也不SMS/MMS (短消息服务/多媒体信息服务)交付是 可用,您可以通知用户或者干脆禁用的消息在您的应用程序 功能。