Mur*_*han 22 reachability ios network-connection swift alamofire
我希望AFNetworking
在Objective-C中使用与Swift中的Alamofire NetworkReachabilityManager 类似的功能:
//Reachability detection
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN: {
[self LoadNoInternetView:NO];
break;
}
case AFNetworkReachabilityStatusReachableViaWiFi: {
[self LoadNoInternetView:NO];
break;
}
case AFNetworkReachabilityStatusNotReachable: {
break;
}
default: {
break;
}
}
}];
Run Code Online (Sandbox Code Playgroud)
我目前正在使用监听器来了解网络状态的变化
let net = NetworkReachabilityManager()
net?.startListening()
Run Code Online (Sandbox Code Playgroud)
有人可以描述如何支持这些用例吗?
Kir*_*ela 34
NetworkManager类
class NetworkManager {
//shared instance
static let shared = NetworkManager()
let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.google.com")
func startNetworkReachabilityObserver() {
reachabilityManager?.listener = { status in
switch status {
case .notReachable:
print("The network is not reachable")
case .unknown :
print("It is unknown whether the network is reachable")
case .reachable(.ethernetOrWiFi):
print("The network is reachable over the WiFi connection")
case .reachable(.wwan):
print("The network is reachable over the WWAN connection")
}
}
// start listening
reachabilityManager?.startListening()
}
}
Run Code Online (Sandbox Code Playgroud)
启动Network Reachability Observer
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// add network reachability observer on app start
NetworkManager.shared.startNetworkReachabilityObserver()
return true
}
}
Run Code Online (Sandbox Code Playgroud)
Mur*_*han 18
我自己找到了答案,即只需编写一个带闭包的监听器,如下所述:
let net = NetworkReachabilityManager()
net?.listener = { status in
if net?.isReachable ?? false {
switch status {
case .reachable(.ethernetOrWiFi):
print("The network is reachable over the WiFi connection")
case .reachable(.wwan):
print("The network is reachable over the WWAN connection")
case .notReachable:
print("The network is not reachable")
case .unknown :
print("It is unknown whether the network is reachable")
}
}
net?.startListening()
Run Code Online (Sandbox Code Playgroud)
rmo*_*ney 16
这是我的实施.我在单身中使用它.请记住保持可访问性管理器参考.
let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.apple.com")
func listenForReachability() {
self.reachabilityManager?.listener = { status in
print("Network Status Changed: \(status)")
switch status {
case .NotReachable:
//Show error state
case .Reachable(_), .Unknown:
//Hide error state
}
}
self.reachabilityManager?.startListening()
}
Run Code Online (Sandbox Code Playgroud)
快速 5
网络状态结构
import Foundation
import Alamofire
struct NetworkState {
var isInternetAvailable:Bool
{
return NetworkReachabilityManager()!.isReachable
}
}
Run Code Online (Sandbox Code Playgroud)
用: -
if (NetworkState().isInternetAvailable) {
// Your code here
}
Run Code Online (Sandbox Code Playgroud)
只要保留reachabilityManager的引用,就可以使用单例
class NetworkStatus {
static let sharedInstance = NetworkStatus()
private init() {}
let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.apple.com")
func startNetworkReachabilityObserver() {
reachabilityManager?.listener = { status in
switch status {
case .notReachable:
print("The network is not reachable")
case .unknown :
print("It is unknown whether the network is reachable")
case .reachable(.ethernetOrWiFi):
print("The network is reachable over the WiFi connection")
case .reachable(.wwan):
print("The network is reachable over the WWAN connection")
}
}
reachabilityManager?.startListening()
}
Run Code Online (Sandbox Code Playgroud)
因此,您可以在应用中的任何位置使用它:
let networkStatus = NetworkStatus.sharedInstance
override func awakeFromNib() {
super.awakeFromNib()
networkStatus.startNetworkReachabilityObserver()
}
Run Code Online (Sandbox Code Playgroud)
您将收到有关网络状态变化的通知.只是为了锦上添花,这是一个非常好的动画,以显示您的互联网连接丢失.
Swift 5:不需要侦听器对象。我们只需要调用闭包:
struct Network {
let manager = Alamofire.NetworkReachabilityManager()
func state() {
manager?.startListening { status in
switch status {
case .notReachable :
print("not reachable")
case .reachable(.cellular) :
print("cellular")
case .reachable(.ethernetOrWiFi) :
print("ethernetOrWiFi")
default :
print("unknown")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以开始使用此功能,例如:
Network().state()
Run Code Online (Sandbox Code Playgroud)