システムがインターネット接続がないことを検出したときにのみ実行する関数と、システムがインターネット接続を検出したときに実行する別の関数が必要です。
私はこのようなことを考えています:
func onInternetConnection() {
//Enable actions
}
func onInternetDisconnection() {
//Disable actions, alert user
}
また、Facebookのメッセンジャーのように、システムが再接続していることを検出する方法も必要です。これにより、システムが再接続していることをユーザーに知らせることができます。
これどうやってするの?
ネットワーク層にMoya / Alamofireを使用しています。
これはAlamofireの場合に機能します
import Alamofire
// In your view did load or in app delegate do like this
let reachabilityManager = NetworkReachabilityManager()
reachabilityManager.listener = { status in
switch status {
case .notReachable:
print("The network is not reachable")
self.onInternetDisconnection()
case .unknown :
print("It is unknown whether the network is reachable")
self.onInternetDisconnection() // not sure what to do for this case
case .reachable(.ethernetOrWiFi):
print("The network is reachable over the WiFi connection")
self.onInternetConnection()
case .reachable(.wwan):
print("The network is reachable over the WWAN connection")
self.onInternetConnection()
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加