iPhone - Detecting SIM card availability

谁说胖子不能爱 提交于 2020-01-20 04:07:07

问题


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. Although it works fine when using a sim card, the returned carrier name if there is no SIM card is the old carrier name. It doesn't detect that the SIM is removed.

I know this contradicts with Apple documentation that if there is no carrier, CTCarrier object shall be nil. But in my app I logged the carrier info and it gives me the latest carrier name although no sim is installed.


回答1:


According to the documentation for [CTCarrier carrierName]:

If you configure a device for a carrier and then remove the SIM card, this property retains the name of the carrier.

As far as I know, you cannot detect if the SIM card is installed. You can only determine if a WWAN connection is available using Reachability.




回答2:


@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;
}



回答3:


The CTCarrier object has 5 properties:

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. isoCountryCode, mobileCountryCode and mobileNetworkCode are reset for iOS 7. That's how you can detect if a SIM is present or not.

For iOS 6 all the values are retained.

I performed the tests using an iPhone 4S and iPhone 5 both running iOS 7.




回答4:


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 true
    }

    return false

}

or

func hasCellularCoverage() -> Bool {

    let networkInfo = CTTelephonyNetworkInfo()

    guard let info = networkInfo.subscriberCellularProvider else {return false}

    return info.isoCountryCode != nil ? true : false

}


来源:https://stackoverflow.com/questions/10488898/iphone-detecting-sim-card-availability

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!