本文主要是介绍[Swift]获取手机SIM卡网络运营商名称,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
导入框架
import CoreTelephony
获取当前运营商名称
/// 获取手机SIM卡网络运营商名称
func currentCarrierName() -> String? {var tempName: String?let info = CTTelephonyNetworkInfo()if #available(iOS 12.0, *) {if let carrierProviders = info.serviceSubscriberCellularProviders {for item in carrierProviders.values {if item.mobileNetworkCode != nil {tempName = item.carrierName}}}} else {if let carrier: CTCarrier = info.subscriberCellularProvider {tempName = carrier.carrierName}}return tempName
}
判断是国内哪家运营商
/// 获取当前手机SIM卡网络运营商名称
func chinaCarrierName() -> String? {var mcc = ""var mnc = ""let info = CTTelephonyNetworkInfo()if #available(iOS 12.0, *) {if let carrierProviders = info.serviceSubscriberCellularProviders {for item in carrierProviders.values {if item.mobileNetworkCode != nil {mcc = item.mobileCountryCode ?? ""mnc = item.mobileNetworkCode ?? ""break}}}} else {if let carrier: CTCarrier = info.subscriberCellularProvider {mcc = carrier.mobileCountryCode ?? ""mnc = carrier.mobileNetworkCode ?? ""}}guard mcc == "460" else { return nil }var tempCarrier: String?if mnc == "00" || mnc == "02" || mnc == "04" || mnc == "07" || mnc == "08" || mnc == "" || mnc == "" {tempCarrier = "mobile"} else if mnc == "01" || mnc == "06" || mnc == "09" {tempCarrier = "unicom"} else if mnc == "03" || mnc == "05" || mnc == "11" {tempCarrier = "telecom"} else if mnc == "15" {tempCarrier = "broadnet"} else if mnc == "20" {tempCarrier = "tietong"}return tempCarrier
}
carrierArr打印结果
Printing description of carrierArr:
▿ 1 element▿ 0 : 2 elements- key : "0000000100000001"- value : CTCarrier (0x28112d9b0) {Carrier name: [中国联通]Mobile Country Code: [460]Mobile Network Code:[01]ISO Country Code:[cn]Allows VOIP? [YES]
}
CTCarrier对象
/** CTCarrier.h* CoreTelephony** Copyright 2009 Apple, Inc.. All rights reserved.**/@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated with no replacement")
open class CTCarrier : NSObject {/** carrierName** Discussion:* An NSString containing the name of the subscriber's cellular service provider.*/@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated; returns '--' at some point in the future")open var carrierName: String? { get }/** mobileCountryCode** Discussion:* An NSString containing the mobile country code for the subscriber's * cellular service provider, in its numeric representation*/@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated; returns '65535' at some point in the future")open var mobileCountryCode: String? { get }/** mobileNetworkCode** Discussion:* An NSString containing the mobile network code for the subscriber's * cellular service provider, in its numeric representation*/@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated; returns '65535' at some point in the future")open var mobileNetworkCode: String? { get }/** isoCountryCode** Discussion:* Returns an NSString object that contains country code for* the subscriber's cellular service provider, represented as an ISO 3166-1* country code string*/@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated; returns '--' at some point in the future")open var isoCountryCode: String? { get }/** allowsVOIP** Discussion:* A BOOL value that is YES if this carrier allows VOIP calls to be* made on its network, NO otherwise.*/@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated; returns YES at some point in the future")open var allowsVOIP: Bool { get }
}
这篇关于[Swift]获取手机SIM卡网络运营商名称的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!