如何确定iOS连接类型?
我想知道我的设备目前是否有Edge,3G,4G或者Wifi可用。
此信息我需要加载不同大小的不同数据。
所以,我不想加载4 MB的连接类型" Edge"。
答案 0 :(得分:18)
对于swift,我们可以使用以下功能:
func getNetworkType()->String {
do{
let reachability:Reachability = try Reachability.reachabilityForInternetConnection()
do{
try reachability.startNotifier()
let status = reachability.currentReachabilityStatus
if(status == .NotReachable){
return ""
}else if (status == .ReachableViaWiFi){
return "Wifi"
}else if (status == .ReachableViaWWAN){
let networkInfo = CTTelephonyNetworkInfo()
let carrierType = networkInfo.currentRadioAccessTechnology
switch carrierType{
case CTRadioAccessTechnologyGPRS?,CTRadioAccessTechnologyEdge?,CTRadioAccessTechnologyCDMA1x?: return "2G"
case CTRadioAccessTechnologyWCDMA?,CTRadioAccessTechnologyHSDPA?,CTRadioAccessTechnologyHSUPA?,CTRadioAccessTechnologyCDMAEVDORev0?,CTRadioAccessTechnologyCDMAEVDORevA?,CTRadioAccessTechnologyCDMAEVDORevB?,CTRadioAccessTechnologyeHRPD?: return "3G"
case CTRadioAccessTechnologyLTE?: return "4G"
default: return ""
}
}else{
return ""
}
}catch{
return ""
}
}catch{
return ""
}
}
答案 1 :(得分:4)
需要CoreTelephony.framework。
let networkInfo = CTTelephonyNetworkInfo()
let networkString = networkInfo.currentRadioAccessTechnology
if networkString == CTRadioAccessTechnologyLTE{
// LTE (4G)
}else if networkString == CTRadioAccessTechnologyWCDMA{
// 3G
}else if networkString == CTRadioAccessTechnologyEdge{
// EDGE (2G)
}
答案 2 :(得分:4)
:
首先导入@import CoreTelephony。
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if(status == NotReachable)
{
//No internet
}
else if (status == ReachableViaWiFi)
{
//WiFi
}
else if (status == ReachableViaWWAN)
{
CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
NSString * carrierType = netinfo.currentRadioAccessTechnology;
if ([carrierType isEqualToString:CTRadioAccessTechnologyGPRS]) {
// @"2G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyEdge]) {
// @"2G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyWCDMA]) {
// @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyHSDPA]) {
// @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyHSUPA]) {
// @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
// @"2G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
// @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
// @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
// @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyeHRPD]) {
// @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyLTE]) {
// @"4G";
}
}
答案 3 :(得分:3)
您可以使用Apple的CTTelephonyNetworkInfo类。您可以在iOS 7+上使用此API
您需要导入它: @import CoreTelephony
答案 4 :(得分:2)
import CoreTelephony
enum RadioAccessTechnology: String {
case cdma = "CTRadioAccessTechnologyCDMA1x"
case edge = "CTRadioAccessTechnologyEdge"
case gprs = "CTRadioAccessTechnologyGPRS"
case hrpd = "CTRadioAccessTechnologyeHRPD"
case hsdpa = "CTRadioAccessTechnologyHSDPA"
case hsupa = "CTRadioAccessTechnologyHSUPA"
case lte = "CTRadioAccessTechnologyLTE"
case rev0 = "CTRadioAccessTechnologyCDMAEVDORev0"
case revA = "CTRadioAccessTechnologyCDMAEVDORevA"
case revB = "CTRadioAccessTechnologyCDMAEVDORevB"
case wcdma = "CTRadioAccessTechnologyWCDMA"
var description: String {
switch self {
case .gprs, .edge, .cdma:
return "2G"
case .lte:
return "4G"
default:
return "3G"
}
}
}
// Example:
let networkInfo = CTTelephonyNetworkInfo()
let networkString = networkInfo.currentRadioAccessTechnology
let tecnology = RadioAccessTechnology(rawValue: networkString)
print(tecnology.description)
答案 5 :(得分:2)
Swift 5,iOS 12.0或更高版本:
import SystemConfiguration
import CoreTelephony
import Foundation
class func getConnectionType() -> String {
guard let reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "www.google.com") else {
return "NO INTERNET"
}
var flags = SCNetworkReachabilityFlags()
SCNetworkReachabilityGetFlags(reachability, &flags)
let isReachable = flags.contains(.reachable)
let isWWAN = flags.contains(.isWWAN)
if isReachable {
if isWWAN {
let networkInfo = CTTelephonyNetworkInfo()
let carrierType = networkInfo.serviceCurrentRadioAccessTechnology
guard let carrierTypeName = carrierType?.first?.value else {
return "UNKNOWN"
}
switch carrierTypeName {
case CTRadioAccessTechnologyGPRS, CTRadioAccessTechnologyEdge, CTRadioAccessTechnologyCDMA1x:
return "2G"
case CTRadioAccessTechnologyLTE:
return "4G"
default:
return "3G"
}
} else {
return "WIFI"
}
} else {
return "NO INTERNET"
}
}
答案 6 :(得分:1)
我发现了一个很好的模块化快速实现,它不依赖任何第三方库,它非常简单,而且轻巧。 HERE可用,但是为了完整起见,下面是代码:
//
// NetStatus.swift
// NetStatusDemo
//
// Created by Gabriel Theodoropoulos.
// Copyright © 2019 Appcoda. All rights reserved.
//
import Foundation
import Network
class NetStatus {
// MARK: - Properties
static let shared = NetStatus()
var monitor: NWPathMonitor?
var isMonitoring = false
var didStartMonitoringHandler: (() -> Void)?
var didStopMonitoringHandler: (() -> Void)?
var netStatusChangeHandler: (() -> Void)?
var isNetStatusConnected: Bool {
guard let monitor = monitor else { return false }
return monitor.currentPath.status == .satisfied
}
var interfaceType: NWInterface.InterfaceType? {
guard let monitor = monitor else { return nil }
return monitor.currentPath.availableInterfaces.filter {
monitor.currentPath.usesInterfaceType($0.type) }.first?.type
}
var availableInterfacesTypes: [NWInterface.InterfaceType]? {
guard let monitor = monitor else { return nil }
return monitor.currentPath.availableInterfaces.map { $0.type }
}
var isExpensive: Bool {
return monitor?.currentPath.isExpensive ?? false
}
// MARK: - Init & Deinit
private init() {
}
deinit {
stopMonitoring()
}
// MARK: - Method Implementation
func startMonitoring() {
guard !isMonitoring else { return }
monitor = NWPathMonitor()
let queue = DispatchQueue(label: "NetStatus_Monitor")
monitor?.start(queue: queue)
monitor?.pathUpdateHandler = { _ in
self.netStatusChangeHandler?()
}
isMonitoring = true
didStartMonitoringHandler?()
}
func stopMonitoring() {
guard isMonitoring, let monitor = monitor else { return }
monitor.cancel()
self.monitor = nil
isMonitoring = false
didStopMonitoringHandler?()
}
}
答案 7 :(得分:0)
快速更新Xcode:10.2.1
更新swift 4.2:
func updateUserInterface() {
switch Network.reachability.status {
case .unreachable:
view.backgroundColor = .black
alertNoReachable("Network Unreachable", "Please check The Settings")
case .wwan:
self.imageNoConnection.isHidden = true
let networkInfo = CTTelephonyNetworkInfo()
let carrierType = networkInfo.serviceCurrentRadioAccessTechnology
//print (carrierType?[0])
// CTRadioAccessTechnologyLTE
if carrierType!.first(where: { (_, value) in value.contains("CTRadioAccessTechnologyLTE") }) != nil {
self.networkType = "4G"
print ("Wwan : 4G")
// view.backgroundColor = .magenta
}
// 2g CTRadioAccessTechnologyGPRS?,CTRadioAccessTechnologyEdge?,CTRadioAccessTechnologyCDMA1x?
else if carrierType!.first(where: { (_, value) in value.contains("CTRadioAccessTechnologyGPRS") }) != nil || carrierType!.first(where: { (_, value) in value.contains("CTRadioAccessTechnologyEdge") }) != nil || carrierType!.first(where: { (_, value) in value.contains("CTRadioAccessTechnologyCDMA1x") }) != nil {
self.networkType = "2G"
print("Wwan : 2G")
//view.backgroundColor = .gray
}
// 3g CTRadioAccessTechnologyWCDMA?,CTRadioAccessTechnologyHSDPA?,CTRadioAccessTechnologyHSUPA?,CTRadioAccessTechnologyCDMAEVDORev0?,CTRadioAccessTechnologyCDMAEVDORevA?,CTRadioAccessTechnologyCDMAEVDORevB?,CTRadioAccessTechnologyeHRPD?
else if carrierType!.first(where: { (_, value) in value.contains("CTRadioAccessTechnologyWCDMA") }) != nil || carrierType!.first(where: { (_, value) in value.contains("CTRadioAccessTechnologyHSDPA") }) != nil || carrierType!.first(where: { (_, value) in value.contains("CTRadioAccessTechnologyHSUPA") }) != nil || carrierType!.first(where: { (_, value) in value.contains("CTRadioAccessTechnologyCDMAEVDORev0") }) != nil || carrierType!.first(where: { (_, value) in value.contains("CTRadioAccessTechnologyCDMAEVDORevA") }) != nil || carrierType!.first(where: { (_, value) in value.contains("CTRadioAccessTechnologyCDMAEVDORevB") }) != nil || carrierType!.first(where: { (_, value) in value.contains("CTRadioAccessTechnologyeHRPD") }) != nil {
self.networkType = "3G"
print ("Wwan : 3G")
//view.backgroundColor = .red
}
case .wifi:
self.imageNoConnection.isHidden = true
self.networkType = "WiFi"
// view.backgroundColor = .green
}
print("Reachability Summary")
print("Status:", Network.reachability.status)
print("HostName:", Network.reachability.hostname ?? "nil")
print("Reachable:", Network.reachability.isReachable)
print("Wifi:", Network.reachability.isReachableViaWiFi)
}
@objc func statusManager(_ notification: Notification) {
updateUserInterface()
}
Reachability.swift
import Foundation
import SystemConfiguration
class Reachability {
var hostname: String?
var isRunning = false
var isReachableOnWWAN: Bool
var reachability: SCNetworkReachability?
var reachabilityFlags = SCNetworkReachabilityFlags()
let reachabilitySerialQueue = DispatchQueue(label: "ReachabilityQueue")
init?(hostname: String) throws {
guard let reachability = SCNetworkReachabilityCreateWithName(nil, hostname) else {
throw Network.Error.failedToCreateWith(hostname)
}
self.reachability = reachability
self.hostname = hostname
isReachableOnWWAN = true
try start()
}
init() throws {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let reachability = withUnsafePointer(to: &zeroAddress, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil, $0)
}
}) else {
throw Network.Error.failedToInitializeWith(zeroAddress)
}
self.reachability = reachability
isReachableOnWWAN = true
try start()
}
var status: Network.Status {
return !isConnectedToNetwork ? .unreachable :
isReachableViaWiFi ? .wifi :
isRunningOnDevice ? .wwan : .unreachable
}
var isRunningOnDevice: Bool = {
#if targetEnvironment(simulator)
return false
#else
return true
#endif
}()
deinit { stop() }
}
extension Reachability {
func start() throws {
guard let reachability = reachability, !isRunning else { return }
var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
context.info = Unmanaged<Reachability>.passUnretained(self).toOpaque()
guard SCNetworkReachabilitySetCallback(reachability, callout, &context) else { stop()
throw Network.Error.failedToSetCallout
}
guard SCNetworkReachabilitySetDispatchQueue(reachability, reachabilitySerialQueue) else { stop()
throw Network.Error.failedToSetDispatchQueue
}
reachabilitySerialQueue.async { self.flagsChanged() }
isRunning = true
}
func stop() {
defer { isRunning = false }
guard let reachability = reachability else { return }
SCNetworkReachabilitySetCallback(reachability, nil, nil)
SCNetworkReachabilitySetDispatchQueue(reachability, nil)
self.reachability = nil
}
var isConnectedToNetwork: Bool {
return isReachable &&
!isConnectionRequiredAndTransientConnection &&
!(isRunningOnDevice && isWWAN && !isReachableOnWWAN)
}
var isReachableViaWiFi: Bool {
return isReachable && isRunningOnDevice && !isWWAN
}
/// Flags that indicate the reachability of a network node name or address, including whether a connection is required, and whether some user intervention might be required when establishing a connection.
var flags: SCNetworkReachabilityFlags? {
guard let reachability = reachability else { return nil }
var flags = SCNetworkReachabilityFlags()
return withUnsafeMutablePointer(to: &flags) {
SCNetworkReachabilityGetFlags(reachability, UnsafeMutablePointer($0))
} ? flags : nil
}
/// compares the current flags with the previous flags and if changed posts a flagsChanged notification
func flagsChanged() {
guard let flags = flags, flags != reachabilityFlags else { return }
reachabilityFlags = flags
NotificationCenter.default.post(name: .flagsChanged, object: self)
}
/// The specified node name or address can be reached via a transient connection, such as PPP.
var transientConnection: Bool { return flags?.contains(.transientConnection) == true }
/// The specified node name or address can be reached using the current network configuration.
var isReachable: Bool { return flags?.contains(.reachable) == true }
/// The specified node name or address can be reached using the current network configuration, but a connection must first be established. If this flag is set, the kSCNetworkReachabilityFlagsConnectionOnTraffic flag, kSCNetworkReachabilityFlagsConnectionOnDemand flag, or kSCNetworkReachabilityFlagsIsWWAN flag is also typically set to indicate the type of connection required. If the user must manually make the connection, the kSCNetworkReachabilityFlagsInterventionRequired flag is also set.
var connectionRequired: Bool { return flags?.contains(.connectionRequired) == true }
/// The specified node name or address can be reached using the current network configuration, but a connection must first be established. Any traffic directed to the specified name or address will initiate the connection.
var connectionOnTraffic: Bool { return flags?.contains(.connectionOnTraffic) == true }
/// The specified node name or address can be reached using the current network configuration, but a connection must first be established.
var interventionRequired: Bool { return flags?.contains(.interventionRequired) == true }
/// The specified node name or address can be reached using the current network configuration, but a connection must first be established. The connection will be established "On Demand" by the CFSocketStream programming interface (see CFStream Socket Additions for information on this). Other functions will not establish the connection.
var connectionOnDemand: Bool { return flags?.contains(.connectionOnDemand) == true }
/// The specified node name or address is one that is associated with a network interface on the current system.
var isLocalAddress: Bool { return flags?.contains(.isLocalAddress) == true }
/// Network traffic to the specified node name or address will not go through a gateway, but is routed directly to one of the interfaces in the system.
var isDirect: Bool { return flags?.contains(.isDirect) == true }
/// The specified node name or address can be reached via a cellular connection, such as EDGE or GPRS.
var isWWAN: Bool { return flags?.contains(.isWWAN) == true }
/// The specified node name or address can be reached using the current network configuration, but a connection must first be established. If this flag is set
/// The specified node name or address can be reached via a transient connection, such as PPP.
var isConnectionRequiredAndTransientConnection: Bool {
return (flags?.intersection([.connectionRequired, .transientConnection]) == [.connectionRequired, .transientConnection]) == true
}
}
func callout(reachability: SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) {
guard let info = info else { return }
DispatchQueue.main.async {
Unmanaged<Reachability>
.fromOpaque(info)
.takeUnretainedValue()
.flagsChanged()
}
}
extension Notification.Name {
static let flagsChanged = Notification.Name("FlagsChanged")
}
struct Network {
static var reachability: Reachability!
enum Status: String {
case unreachable, wwan, wifi
}
enum Error: Swift.Error {
case failedToSetCallout
case failedToSetDispatchQueue
case failedToCreateWith(String)
case failedToInitializeWith(sockaddr_in)
}
}
AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// network reachable
do {
try Network.reachability = Reachability(hostname: "www.google.com")
}
catch {
switch error as? Network.Error {
case let .failedToCreateWith(hostname)?:
print("Network error:\nFailed to create reachability object With host named:", hostname)
case let .failedToInitializeWith(address)?:
print("Network error:\nFailed to initialize reachability object With address:", address)
case .failedToSetCallout?:
print("Network error:\nFailed to set callout")
case .failedToSetDispatchQueue?:
print("Network error:\nFailed to set DispatchQueue")
case .none:
print(error)
}
}
//
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
答案 8 :(得分:0)
我使用iOS 12.0+ serviceCurrentRadioAccessTechnology和switch语句找到了适合我的需要的实现。
@objc var getCellularNetworkType: String {
guard (monitor != nil) else { return "" }
guard isCellular else { return "" }
let networkInfo = CTTelephonyNetworkInfo()
let carrierTypeString = networkInfo.serviceCurrentRadioAccessTechnology!.values.first!
switch carrierTypeString {
case CTRadioAccessTechnologyGPRS,CTRadioAccessTechnologyEdge,CTRadioAccessTechnologyCDMA1x: return "2G"
case CTRadioAccessTechnologyWCDMA,CTRadioAccessTechnologyHSDPA,CTRadioAccessTechnologyHSUPA,CTRadioAccessTechnologyCDMAEVDORev0,CTRadioAccessTechnologyCDMAEVDORevA,CTRadioAccessTechnologyCDMAEVDORevB,CTRadioAccessTechnologyeHRPD: return "3G"
case CTRadioAccessTechnologyLTE: return "4G"
default: return ""
}
}