当我试着在我的iPhone上检查网络连接时,我得到了一堆错误。有人能帮我解决这个问题吗?
代码:
import Foundation
import SystemConfiguration
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}
var flags: SCNetworkReachabilityFlags = 0
if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
return false
}
let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection) ? true : false
}
}
代码的错误:
如果它是不可读的,错误1说:
'Int'不能转换为'SCNetworkReachabilityFlags'
错误2和3:
找不到一个超载的'init'接受提供的参数
我改进了莱科斯的例子。我添加了一些额外的控件来解决双重触发问题,还添加了通知支持来侦听状态更改。
我为防止双重触发问题而添加的控件还显示了设备主要使用哪个连接源来访问互联网。
例如,即使设备同时连接到蜂窝网络和Wi-Fi,“状态”返回为“connectedViaWiFi”,以指示当前的互联网访问是通过Wi-Fi。
import Foundation
import Network
class Reachability {
enum StatusFlag {
case unknow
case noConnection
case connectedViaWiFi
case connectedViaCellular
}
static let connectionStatusHasChangedNotification = NSNotification.Name("Reachability.connectionStatusHasChangedNotification")
static let shared = Reachability()
private var monitorForWifi: NWPathMonitor?
private var monitorForCellular: NWPathMonitor?
private var wifiStatus: NWPath.Status = .requiresConnection
private var cellularStatus: NWPath.Status = .requiresConnection
private var ignoreInitialWiFiStatusUpdate: Bool = true
private var ignoreInitialCelluluarStatusUpdate: Bool = true
private var isReachableOnCellular: Bool { cellularStatus == .satisfied }
private var isReachableOnWiFi: Bool { wifiStatus == .satisfied }
var status: StatusFlag = .unknow {
didSet {
guard status != oldValue else { return }
DispatchQueue.main.async { [weak self] in
NotificationCenter.default.post(name: Self.connectionStatusHasChangedNotification,
object: self?.status)
}
}
}
func startMonitoring() {
monitorForWifi = NWPathMonitor(requiredInterfaceType: .wifi)
monitorForWifi?.pathUpdateHandler = { [weak self] path in
self?.wifiStatus = path.status
self?.ignoreInitialWiFiStatusUpdate = false
self?.updateStatus()
}
monitorForCellular = NWPathMonitor(requiredInterfaceType: .cellular)
monitorForCellular?.pathUpdateHandler = { [weak self] path in
self?.cellularStatus = path.status
self?.ignoreInitialCelluluarStatusUpdate = false
self?.updateStatus()
}
let queue = DispatchQueue.global(qos: .background)
monitorForCellular?.start(queue: queue)
monitorForWifi?.start(queue: queue)
}
func stopMonitoring() {
monitorForWifi?.cancel()
monitorForWifi = nil
monitorForCellular?.cancel()
monitorForCellular = nil
wifiStatus = .requiresConnection
cellularStatus = .requiresConnection
status = .unknow
ignoreInitialWiFiStatusUpdate = true
ignoreInitialCelluluarStatusUpdate = true
}
private func updateStatus() {
if ignoreInitialWiFiStatusUpdate || ignoreInitialCelluluarStatusUpdate {
return
}
if !(isReachableOnCellular && isReachableOnWiFi) {
if isReachableOnCellular && !isReachableOnWiFi {
status = .connectedViaCellular
} else if isReachableOnWiFi && !isReachableOnCellular {
status = .connectedViaWiFi
} else {
status = .noConnection
}
} else {
status = .connectedViaWiFi
}
}
static func isConnectedToNetwork() -> Bool {
return shared.isReachableOnCellular || shared.isReachableOnWiFi
}
}
示例使用
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(_:)), name: Reachability.connectionStatusHasChangedNotification, object: nil)
Reachability.shared.startMonitoring()
}
@objc func reachabilityChanged(_ sender: Notification) {
guard let statusFlag = sender.object as? Reachability.StatusFlag else { return }
print("TEST -> statusFlag: \(statusFlag)")
}
这是与接受的答案相同的代码,但我发现在某些情况下使用闭包更有用
import SystemConfiguration
public class Reachability {
class func isConnectedToNetwork(isConnected : (Bool) -> ()) {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
isConnected(false)
}
/* Only Working for WIFI
let isReachable = flags == .reachable
let needsConnection = flags == .connectionRequired
return isReachable && !needsConnection
*/
// Working for Cellular and WIFI
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
let ret = (isReachable && !needsConnection)
isConnected(ret)
}
}
下面是如何使用它:
Reachability.isConnectedToNetwork { (isConnected) in
if isConnected {
//We have internet connection | get data from server
} else {
//We don't have internet connection | load from database
}
}
我使用NSTimer和Alamofire制作了自己的解决方案:
import Alamofire
public class ConnectionHelper: NSObject {
var request: Alamofire.Request?
func isInternetConnected(completionHandler: Bool -> Void) {
NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "requestTimeout", userInfo: nil, repeats: false)
request = Alamofire
.request(
Method.HEAD,
"http://www.testurl.com"
)
.response { response in
if response.3?.code == -999 {
completionHandler(
false
)
} else {
completionHandler(
true
)
}
}
}
func requestTimeout() {
request!.cancel()
}
}
NSTimer被用作超时,并且由于使用Alamofire超时的不可靠结果而被使用。请求应该发送到您认为可靠的URL,例如您自己的服务器或托管您所依赖的服务的服务器。
当计时器过期时,请求将被取消,并使用完成处理程序返回结果。
用法:
ConnectionHelper().isInternetConnected() { internetConnected in
if internetConnected {
// Connected
} else {
// Not connected
}
}
在项目中创建一个新的Swift文件,命名为Reachability.swift。将以下代码剪切并粘贴到其中以创建您的类。
import Foundation
import SystemConfiguration
open class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}
var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
return false
}
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection)
}
}
你可以在代码的任何地方调用Reachability,就像
if Reachability.isConnectedToNetwork() {
print("Network is connected")
} else {
print("Network is not connected")
}
iOS12 Swift 4和Swift 5
如果你只是想检查连接,你的最低目标是iOS12,那么你可以使用NWPathMonitor
import Network
它需要一些属性的设置。
let internetMonitor = NWPathMonitor()
let internetQueue = DispatchQueue(label: "InternetMonitor")
private var hasConnectionPath = false
我创建了一个函数来启动它。你可以在viewdidload或其他地方做这个。我派了个保镖,你想怎么打就怎么打。
func startInternetTracking() {
// only fires once
guard internetMonitor.pathUpdateHandler == nil else {
return
}
internetMonitor.pathUpdateHandler = { update in
if update.status == .satisfied {
print("Internet connection on.")
self.hasConnectionPath = true
} else {
print("no internet connection.")
self.hasConnectionPath = false
}
}
internetMonitor.start(queue: internetQueue)
}
/// will tell you if the device has an Internet connection
/// - Returns: true if there is some kind of connection
func hasInternet() -> Bool {
return hasConnectionPath
}
现在只需调用helper函数hasInternet()来查看是否有。它实时更新。请参阅Apple文档中的NWPathMonitor。它有更多的功能,如取消(),如果你需要停止跟踪连接,你正在寻找的互联网类型等。
https://developer.apple.com/documentation/network/nwpathmonitor
这里是我的解决方案swift 2.3与库(reach .swift)
进入你的Podfile并添加:
pod 'ReachabilitySwift', '~> 2.4' // swift 2.3
然后进入您的终端:
pod install
然后创建一个新的文件ReachabilityManager并添加下面的代码:
import Foundation
import ReachabilitySwift
enum ReachabilityManagerType {
case Wifi
case Cellular
case None
}
class ReachabilityManager {
static let sharedInstance = ReachabilityManager()
private var reachability: Reachability!
private var reachabilityManagerType: ReachabilityManagerType = .None
private init() {
do {
self.reachability = try Reachability.reachabilityForInternetConnection()
} catch {
print("Unable to create Reachability")
return
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ReachabilityManager.reachabilityChanged(_:)),name: ReachabilityChangedNotification,object: self.reachability)
do{
try self.reachability.startNotifier()
}catch{
print("could not start reachability notifier")
}
}
@objc private func reachabilityChanged(note: NSNotification) {
let reachability = note.object as! Reachability
if reachability.isReachable() {
if reachability.isReachableViaWiFi() {
self.reachabilityManagerType = .Wifi
} else {
self.reachabilityManagerType = .Cellular
}
} else {
self.reachabilityManagerType = .None
}
}
}
extension ReachabilityManager {
func isConnectedToNetwork() -> Bool {
return reachabilityManagerType != .None
}
}
如何使用:
进入你的AppDelegate.swift并添加下面的代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
ReachabilityManager.sharedInstance
}
然后,当你想检查设备是否连接到互联网时,请:
if ReachabilityManager.sharedInstance.isConnectedToNetwork() {
// Connected
} else {
// Not connected
}