简体   繁体   中英

How to read SIM Carrier name programmatically in iOS using Swift4

Before going in-depth, let me tell you all that - Yes, I've gone through all possible solutions provided on Stack-Overflow.

Problem Statement: I'm not able to read 'CarrierName' of my available SIM using iPhone

What did I tried: I've tried two different solutions, but I'm unable to read CarrierName.

Solution 1: When I tried this solution I've received only "Carrier" as output, instead of CarrierName.

Solution 1:

//--------------- CodeBase : Solution1 -----------------

let networkInfo = CTTelephonyNetworkInfo()
let carrier = networkInfo.serviceSubscriberCellularProviders?.first?.value

  if let carrierName = carrier?.carrierName {
          cell.textLabel?.text = carrierName
    }
    else{
           cell.textLabel?.text = "No Data"
  }
//-------------------------------------------------------

Output: Carrier

Solution 2: When I tried this solution I've received "iPhone X" as output, instead of CarrierName.

Solution 2:

//--------------- CodeBase : Solution2 -----------------

let networkInfo = CTTelephonyNetworkInfo()
let carrier = networkInfo.serviceSubscriberCellularProviders?.first?.value

        if var carrierName = carrier?.carrierName {
                
                if carrierName.contains("Carrier"){
                    carrierName = self.getCarrierName() ?? "No Data"
                }
                else{
                    cell.textLabel?.text = carrierName
                }
            }
            else{
                
               cell.textLabel?.text = "No Data"
            }
//-------------------------------------------------------

Func: getCarrierName()

//--------------- CodeBase : Part of Solution2 -----------------

func getCarrierName() -> String? {
        
        var carrierName: String?
        
        let typeName: (Any) -> String = { String(describing: type(of: $0)) }
        
        let statusBar = UIApplication.shared.value(forKey: "_statusBar") as! UIView
        
        for statusBarForegroundView in statusBar.subviews {
            if typeName(statusBarForegroundView) == "UIStatusBarForegroundView" {
                for statusBarItem in statusBarForegroundView.subviews {
                    if typeName(statusBarItem) == "UIStatusBarServiceItemView" {
                        carrierName = (statusBarItem.value(forKey: "_serviceString") as! String)
                    }
                }
            }
        }
        return carrierName
    }
//-------------------------------------------------------


Output: iPhone X ie It returns ModelName instead of CarrierName

Can someone, please help me to get - CarrierName.

In my case ( Xcode 11.4, Swift 5.2, iPhone 8, iOS 13.3.1 ), I can get the proper carrier name.

Code snippet:

import CoreTelephony

if #available(iOS 12.0, *) {
    if let providers = CTTelephonyNetworkInfo().serviceSubscriberCellularProviders {
        providers.forEach { (key, value) in
            print("key: \(key), carrier: \(value.carrierName ?? "nil")")
        }
    }
} else {
    let provider = CTTelephonyNetworkInfo().subscriberCellularProvider
    print("carrier: \(provider?.carrierName ?? "nil")")
}

The result in console is:

key: 0000000100000001, carrier: 中国电信

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM