好吧,我对 Swift 还比较陌生,我对自己想做什么或者是否走错了方向感到有点困惑。 ( https://github.com/ashleymills/Reachability.swift )
这是我的 viewDidLoad
方法:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
checkConnection()
}
然后我有一个函数,代码来自 Reachability GitHub 项目:
func checkConnection() {
//declare this property where it won't go out of scope relative to your listener
let reachability = Reachability()!
reachability.whenReachable = { reachability in
// this is called on a background thread, but UI updates must
// be on the main thread, like this:
DispatchQueue.main.async {
if reachability.isReachableViaWiFi {
print("Reachable via WiFi")
} else {
print("Reachable via Cellular")
}
}
}
reachability.whenUnreachable = { reachability in
// this is called on a background thread, but UI updates must
// be on the main thread, like this:
DispatchQueue.main.async {
self.dim(direction: .In, alpha: self.dimLevel, speed: self.dimSpeed)
self.performSegue(withIdentifier: "mainToAlertNoInternet", sender: self)
}
}
do {
try reachability.startNotifier()
} catch {
print("Unable to start notifier")
}
}
如您所见,当没有互联网时,这是代码:
self.dim(direction: .In, alpha: self.dimLevel, speed: self.dimSpeed)
self.performSegue(withIdentifier: "mainToAlertNoInternet", sender: self)
Dim 取自 ( http://www.totem.training/swift-ios-tips-tricks-tutorials-blog/ux-chops-dim-the-lights ) mainToAlertNoInternet
以透明方式在当前 View 上加载下一个 View ,因此它是一种警报样式。
因此,第二个 segue 上有一个 View 和一个按钮。没什么了不起的,这是在没有互联网时加载的内容:
重试按钮链接到 Segue 的退出并在第一个 View Controller 中运行此函数:
@IBAction func unwindFromSecondary(segue: UIStoryboardSegue) {
dim(direction: .Out, speed: dimSpeed)
checkInternetConnection()
}
我在函数 mainToAlertNoInternet
中添加了这样当他们点击重试时,它将返回到第一个 segue 并再次运行测试。然而,当我点击重试时,我得到了这个错误:
Warning: Attempt to present on whose view is not in the window hierarchy!
希望我已经充分解释了我的设置。现在回答问题:
1) 我该如何解决这个错误? 2) 我这样做的方式正确还是有更好的方式?
这就是我想要的:
我想在应用加载时检查互联网连接。如果没有连接,我想像我一直在做的那样显示 segue。如果用户单击 Try gain,我希望它返回到第一个 Controller 并再次运行检查,如果仍然没有连接,则像最初那样再次显示 segue。我希望这是一个重复的过程,直到有互联网为止。
感谢您的所有帮助。提前致谢
编辑:
我在 ViewDidAppear 方法中添加了函数调用,如下所示:
override func viewDidAppear(_ animated: Bool) {
checkInternetConnection()
}
但是,它没有运行。当我这样做时,unwindFromSecondary
函数中的 DIM 也不会被调用。
编辑 2:
刚刚将这一行添加到我的 viewDidAppear
中:
print("Appeared")
这最初会被调用,但之后不会再次调用。
问题:
在 unwindSegue 之后再次加载所有内容后,如何让函数运行?
有什么想法吗?
更新 3
好的,所以我看了下面的答案:
@MarkP答案很好。谢谢你 然而,来自@iSee的答案让我想到也许我应该以不同的方式解决这个问题。
我在这篇文章中添加了赏金以获得详细的答案,可以向我展示并解释如何实现以下目标:
在我的应用程序中。我需要确保在加载的任何 View 上都存在互联网(也许是计时器)。我希望它像目前的方式一样,如果没有互联网,它会弹出带有此 segue 的 ViewController:
performSegue(withIdentifier: "mainToAlertNoInernet", sender: self)
App Delegate 似乎是合适的地方,但我不确定如何实现。
我是 iOS 开发的新手,因此希望得到一些解释和指导。
感谢您的宝贵时间。非常感谢。
最佳答案
我假设您是 iOS 开发的新手。
该警告与您的互联网连接代码无关。
您收到的警告不是错误。就是这样,一个警告。
- 你得到它的原因在这个 link 中得到了很好的解释。和 this
- 要消除该警告,您不应从
viewDidLoad
调用 performSegue(有关详细信息,请参阅上面的链接。 - 要执行网络检查,建议使用 AppDelegate(让您更好地控制应用程序的流程)
祝一切顺利:)
编辑:请引用this在此处链接以获取更多信息。我可以很容易地在此处重新发布它,但由于它已经得到解答,您可以引用上面的链接了解它发生的原因以及如何避免它。
关于ios - swift 3 : Checking Internet (Reocurring) viewDidAppear not,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39547851/