vin*_*nan 7 objective-c ios core-telephony ios6 ios7
我有一个能够发送消息的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");
}
}
Run Code Online (Sandbox Code Playgroud)
我没有使用SIM卡检查过我的iphone,它总是返回@"Sim available".但是,当我打开默认的"消息"应用程序并尝试发送短信时,它会显示警告"未安装SIM卡"...此消息应用程序如何检测SIM卡可用性?
您可以通过CTCarrier课程进行检查.

BOOL isSimCardAvailable = YES;
CTTelephonyNetworkInfo* info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier* carrier = info.subscriberCellularProvider;
if(carrier.mobileNetworkCode == nil || [carrier.mobileNetworkCode isEqualToString:@""])
{
isSimCardAvailable = NO;
}
Run Code Online (Sandbox Code Playgroud)
您需要添加CoreTelephony框架以使用CTTelephonyNetworkInfo和CTCarrier.
通过以下代码,您可以获得sim卡详细信息,如carriername,mobilecountrycode,isocountrycode,mobilenetworkcode.在ios 6中都保留.所以如果你的SIM卡被删除也会保留旧的细节.所以这个想法不会有用但是在ios 7中只保留了运营商名称并保留了其余名称,因此可以使用以下代码
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");
});
};
Run Code Online (Sandbox Code Playgroud)
在Swift中作为只读计算属性实现:
import CoreTelephony
var availableSIM: Bool {
return CTTelephonyNetworkInfo().subscriberCellularProvider?.mobileNetworkCode != nil
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8363 次 |
| 最近记录: |