我有一个能够发送消息的iPhone应用程序。我想在iphone中没有SIM卡时提醒用户。所以我尝试了以下三个功能来检查SIM卡的可用性
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if([messageClass canSendText]){
// Sim available
NSLog(@"Sim available");
}
else{
//Sim not available
NSLog(@"Sim not available");
}
if([MFMessageComposeViewController canSendText]){
// Sim available
NSLog(@"Sim available");
}
else{
//Sim not available
NSLog(@"Sim not available");
}
if([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel:123456"]])
{
NSLog(@"Sim available");
}
else
{
NSLog(@"Sim not available");
}
}
我已经检查了我的iphone没有SIM卡,它总是返回@“Sim available”。但是,当我打开默认的“消息”应用程序并尝试发送短信时,它会显示警告“未安装SIM卡”...此消息应用程序如何检测SIM卡的可用性?
答案 0 :(得分:7)
您可以通过CTCarrier课程进行检查。
BOOL isSimCardAvailable = YES;
CTTelephonyNetworkInfo* info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier* carrier = info.subscriberCellularProvider;
if(carrier.mobileNetworkCode == nil || [carrier.mobileNetworkCode isEqualToString:@""])
{
isSimCardAvailable = NO;
}
您需要添加CoreTelephony框架以使用CTTelephonyNetworkInfo和CTCarrier。
答案 1 :(得分:6)
通过以下代码,您可以获得sim卡详细信息,如carriername,mobilecountrycode,isocountrycode,mobilenetworkcode。在ios 6中都保留。所以如果你的SIM卡被删除也会保留旧的细节。所以有这个想法不会有用,但在ios 7中只保留了carriername并且保留了更改,因此可以使用下面的代码
CTTelephonyNetworkInfo* info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier* carrier = info.subscriberCellularProvider;
NSString *mobileCountryCode = carrier.mobileCountryCode;
NSString *carrierName = carrier.carrierName;
NSString *isoCountryCode = carrier.isoCountryCode;
NSString *mobileNetworkCode = carrier.mobileNetworkCode
// Try this to track CTCarrier changes
info.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier* inCTCarrier) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"User did change SIM");
});
};
答案 2 :(得分:6)
在Swift中实现为只读计算属性:
import CoreTelephony
var availableSIM: Bool {
return CTTelephonyNetworkInfo().subscriberCellularProvider?.mobileNetworkCode != nil
}
答案 3 :(得分:2)
补充Anneq Anwar的答案,这里是swift版本:
import CoreTelephony
func isSimAvailable() -> Bool {
var isSimCardAvailable = true
var info = CTTelephonyNetworkInfo()
var carrier = info.subscriberCellularProvider
if carrier != nil && carrier.mobileNetworkCode == nil || carrier.mobileNetworkCode.isEqual("") {
isSimCardAvailable = false
}
return isSimCardAvailable
}
修复:某些设备中的运营商可能为零
答案 4 :(得分:2)
对Anneq和Felipe的补充答案:
Swift 2.3:
import CoreTelephony
func isSimAvailable() -> Bool {
let info = CTTelephonyNetworkInfo()
let carr = info.subscriberCellularProvider
guard let carrier = carr else {
return false
}
guard let carrierCode = carrier.mobileNetworkCode else {
return false
}
guard carrierCode != "" else {
return false
}
return true
}
答案 5 :(得分:0)
快速4 +
var isSimCardAvailable: Bool {
let info = CTTelephonyNetworkInfo()
if let carrier = info.subscriberCellularProvider {
if let code = carrier.mobileNetworkCode {
if !code.isEmpty {
return true
}
}
}
return false
}