3
iOSアプリケーションで次の機能を使用してFirebaseに接続しているかどうかを確認します。この関数が初めてアプリケーションで実行されるとき、(たとえFirebaseに接続されていても)接続状態に対して常にfalseを返します。 「インターネットなし」アラートを終了し、関数を再度呼び出すと、正しい接続状態が返されます(true)。最初の実行時にFirebase接続状態が常にfalseを返す
Firebaseが初めて機能を実行したときにtrue
の接続状態を返さないことは知っていますか?
func checkInternetOnLoad(){
let connectedRef = FIRDatabase.database().reference(withPath: ".info/connected")
connectedRef.observeSingleEvent(of: .value, with: { (snapshot) in
// Log snapshot value (Note: this is always false the first time the function is run
print(snapshot.value as? Bool!)
if let connected = snapshot.value as? Bool{
if !connected{
// We don't have internet
// Show alert saying we don't have internet. Pressing "Try Now" reruns the function to check internet again.
let alert = showError(title: "Your Phone's Offline", message: "If you're online and seeing this message, please contact support.", cancelMessage: "Try now", cancelAction: {
self.checkInternetOnLoad()
})
self.present(alert, animated: true, completion: nil)
} else {
// We have internet. Do stuff
}
}
})
}
値は正確には何であると思われますか?実行時に変更されますか?ユーザー固有ですか?あなたがインターネット接続を確認するためにそれを使用している場合、それは良い考えではありません。 – Starlord