在项目开发中,有时需要获取当前设备的运营商信息(是电信、还是联通、移动)。又或者想知道当前设备使用的移动网络制式(4G、3G、还是 2G)。这个借助系统的 CoreTelephony 框架就能够实现。
1、效果
image.png
2、样例代码
//
// ViewController.swift
// networkType
//
// Created by 李东 on 2020/11/16.
//
import UIKit
import CoreTelephony
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//获取并输出运营商信息
let info = CTTelephonyNetworkInfo()
if let carrier = info.subscriberCellularProvider {
let currentRadioTech = info.currentRadioAccessTechnology!
print("数据业务信息:\(currentRadioTech)")
print("网络制式:\(getNetworkType(currentRadioTech: currentRadioTech))")
print("运营商名字:\(carrier.carrierName!)")
print("移动国家码(MCC):\(carrier.mobileCountryCode!)")
print("移动网络码(MNC):\(carrier.mobileNetworkCode!)")
print("ISO国家代码:\(carrier.isoCountryCode!)")
print("是否允许VoIP:\(carrier.allowsVOIP)")
}
}
//根据数据业务信息获取对应的网络类型
func getNetworkType(currentRadioTech:String) -> String {
var networkType = ""
switch currentRadioTech {
case CTRadioAccessTechnologyGPRS:
networkType = "2G"
case CTRadioAccessTechnologyEdge:
networkType = "2G"
case CTRadioAccessTechnologyeHRPD:
networkType = "3G"
case CTRadioAccessTechnologyHSDPA:
networkType = "3G"
case CTRadioAccessTechnologyCDMA1x:
networkType = "2G"
case CTRadioAccessTechnologyLTE:
networkType = "4G"
case CTRadioAccessTechnologyCDMAEVDORev0:
networkType = "3G"
case CTRadioAccessTechnologyCDMAEVDORevA:
networkType = "3G"
case CTRadioAccessTechnologyCDMAEVDORevB:
networkType = "3G"
case CTRadioAccessTechnologyHSUPA:
networkType = "3G"
default:
break
}
return networkType
}
}
原文出自:www.hangge.com 转载请保留原文链接:https://www.hangge.com/blog/cache/detail_1607.html
网友评论