[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

相关文章

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

录音功能在哪里? 电脑手机等设备打开录音功能的技巧

《录音功能在哪里?电脑手机等设备打开录音功能的技巧》很多时候我们需要使用录音功能,电脑和手机这些常用设备怎么使用录音功能呢?下面我们就来看看详细的教程... 我们在会议讨论、采访记录、课堂学习、灵感创作、法律取证、重要对话时,都可能有录音需求,便于留存关键信息。下面分享一下如何在电脑端和手机端上找到录音功能

Nginx屏蔽服务器名称与版本信息方式(源码级修改)

《Nginx屏蔽服务器名称与版本信息方式(源码级修改)》本文详解如何通过源码修改Nginx1.25.4,移除Server响应头中的服务类型和版本信息,以增强安全性,需重新配置、编译、安装,升级时需重复... 目录一、背景与目的二、适用版本三、操作步骤修改源码文件四、后续操作提示五、注意事项六、总结一、背景与

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1