[Swift]获取手机SIM卡网络运营商名称

2024-08-20 22:38

本文主要是介绍[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卡网络运营商名称的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1091372

相关文章

springboot的controller中如何获取applicatim.yml的配置值

《springboot的controller中如何获取applicatim.yml的配置值》本文介绍了在SpringBoot的Controller中获取application.yml配置值的四种方式,... 目录1. 使用@Value注解(最常用)application.yml 配置Controller 中

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

golang实现nacos获取配置和服务注册-支持集群详解

《golang实现nacos获取配置和服务注册-支持集群详解》文章介绍了如何在Go语言中使用Nacos获取配置和服务注册,支持集群初始化,客户端结构体中的IpAddresses可以配置多个地址,新客户... 目录golang nacos获取配置和服务注册-支持集群初始化客户端可选参数配置new一个客户端 支

sqlserver、mysql、oracle、pgsql、sqlite五大关系数据库的对象名称和转义字符

《sqlserver、mysql、oracle、pgsql、sqlite五大关系数据库的对象名称和转义字符》:本文主要介绍sqlserver、mysql、oracle、pgsql、sqlite五大... 目录一、转义符1.1 oracle1.2 sqlserver1.3 PostgreSQL1.4 SQLi

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

Java发送SNMP至交换机获取交换机状态实现方式

《Java发送SNMP至交换机获取交换机状态实现方式》文章介绍使用SNMP4J库(2.7.0)通过RCF1213-MIB协议获取交换机单/多路状态,需开启SNMP支持,重点对比SNMPv1、v2c、v... 目录交换机协议SNMP库获取交换机单路状态获取交换机多路状态总结交换机协议这里使用的交换机协议为常

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

C#使用iText获取PDF的trailer数据的代码示例

《C#使用iText获取PDF的trailer数据的代码示例》开发程序debug的时候,看到了PDF有个trailer数据,挺有意思,于是考虑用代码把它读出来,那么就用到我们常用的iText框架了,所... 目录引言iText 核心概念C# 代码示例步骤 1: 确保已安装 iText步骤 2: C# 代码程

Python实现简单封装网络请求的示例详解

《Python实现简单封装网络请求的示例详解》这篇文章主要为大家详细介绍了Python实现简单封装网络请求的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录安装依赖核心功能说明1. 类与方法概览2.NetHelper类初始化参数3.ApiResponse类属性与方法使用实

Spring Boot中获取IOC容器的多种方式

《SpringBoot中获取IOC容器的多种方式》本文主要介绍了SpringBoot中获取IOC容器的多种方式,包括直接注入、实现ApplicationContextAware接口、通过Spring... 目录1. 直接注入ApplicationContext2. 实现ApplicationContextA