[英]iPhone - Detecting SIM card availability
I am using the answer in this topic.我正在使用本主题中的答案。 iPhone - how to determine carrier of the device (AT&T, Verizon, etc?) which is the same as getting operator details in iphone . iPhone - 如何确定设备的运营商(AT&T、Verizon 等?) ,这与在 iphone 中获取运营商详细信息相同。 Although it works fine when using a sim card, the returned carrier name if there is no SIM card is the old carrier name.虽然使用sim卡可以正常工作,但是如果没有SIM卡,返回的运营商名称是旧的运营商名称。 It doesn't detect that the SIM is removed.它不会检测到 SIM 已被移除。
I know this contradicts with Apple documentation that if there is no carrier, CTCarrier object shall be nil.我知道这与 Apple 文档相矛盾,即如果没有运营商,则 CTCarrier 对象应为零。 But in my app I logged the carrier info and it gives me the latest carrier name although no sim is installed.但是在我的应用程序中,我记录了运营商信息,虽然没有安装 SIM 卡,但它给了我最新的运营商名称。
According to the documentation for [CTCarrier carrierName]
:根据[CTCarrier carrierName]
的文档:
If you configure a device for a carrier and then remove the SIM card, this property retains the name of the carrier.如果您为运营商配置设备,然后移除 SIM 卡,则此属性保留运营商的名称。
As far as I know, you cannot detect if the SIM card is installed.据我所知,您无法检测是否安装了 SIM 卡。 You can only determine if a WWAN connection is available using Reachability
.您只能使用Reachability
确定 WWAN 连接是否可用。
@import CoreTelephony;
-(BOOL)hasCellularCoverage
{
CTTelephonyNetworkInfo *networkInfo = [CTTelephonyNetworkInfo new];
CTCarrier *carrier = [networkInfo subscriberCellularProvider];
if (!carrier.isoCountryCode) {
NSLog(@"No sim present Or No cellular coverage or phone is on airplane mode.");
return NO;
}
return YES;
}
The CTCarrier object has 5 properties: CTCarrier对象有 5 个属性:
allowsVOIP
carrierName
isoCountryCode
mobileCountryCode
mobileNetworkCode
I have made some tests regarding CTCarrier and I have come to the conclusion that for iOS 7 only carrierName and allowsVOIP are retained when SIM is removed.我已经对 CTCarrier 进行了一些测试,我得出的结论是,对于 iOS 7 ,移除 SIM 卡时仅会保留carrierName 和 allowedVOIP 。 isoCountryCode, mobileCountryCode and mobileNetworkCode are reset for iOS 7. That's how you can detect if a SIM is present or not. isoCountryCode、mobileCountryCode 和 mobileNetworkCode 已针对 iOS 7 重置。这就是您可以检测 SIM 是否存在的方法。
For iOS 6 all the values are retained.对于 iOS 6,保留所有值。
I performed the tests using an iPhone 4S and iPhone 5 both running iOS 7.我使用运行 iOS 7 的 iPhone 4S 和 iPhone 5 进行了测试。
Swift version:迅捷版:
func hasCellularCoverage() -> Bool {
let networkInfo = CTTelephonyNetworkInfo()
guard let info = networkInfo.subscriberCellularProvider else {return false}
if let carrier = info.isoCountryCode {
print("No sim present Or No cellular coverage or phone is on airplane mode. Carrier = \(carrier)");
return false
}
return true
}
or要么
func hasCellularCoverage() -> Bool {
let networkInfo = CTTelephonyNetworkInfo()
guard let info = networkInfo.subscriberCellularProvider else {return false}
return info.isoCountryCode != nil ? false : true
}
Swift 5.4 answer斯威夫特 5.4 答案
Accepted answer is outdated or incorrect.接受的答案已过时或不正确。
protocol SimCardServiceProtocol {
var isAvailableSIM: Bool { get }
}
final class SimCardService: SimCardServiceProtocol {
var isAvailableSIM: Bool {
return CTTelephonyNetworkInfo().serviceSubscriberCellularProviders?.first?.value.mobileNetworkCode != nil
}
}
hope this helps:希望这可以帮助:
if #available(iOS 12.0, *) {
return CTTelephonyNetworkInfo().serviceSubscriberCellularProviders?.first?.value.mobileNetworkCode != nil
} else {
if let _ = CTTelephonyNetworkInfo().subscriberCellularProvider?.isoCountryCode {
return true
} else {
return false
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.