1
我想连接到互联网。如果5秒后我没有连接(使用可达性)我想中止。这可能吗 ?互联网连接 - 我如何尝试连接5秒,然后放弃?
我想连接到互联网。如果5秒后我没有连接(使用可达性)我想中止。这可能吗 ?互联网连接 - 我如何尝试连接5秒,然后放弃?
NSTimer具有1秒的回调和计数器。如果5次尝试失败,则5秒钟过去。
喜欢的东西:
var timerCounter = 0
var timer : NSTimer?
func shouldAttemptConnection() {
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "attemptConnection", userInfo: nil, repeats: true)
self.attemptConnection()
}
func attemptConnection() {
if Reachability.isReachable() {
// Handle success code
} else if ++timerCounter >= 5 {
// Handle timeout code
} else {
// Try again
return
}
self.invalidateTimer()
}
func invalidateTimer() {
timer?.invalidate()
timer = nil
timerCounter = 0
}
如果你的问题是关于一个特定的语言或框架,但如果添加一个标签为语言或框架您的问题发帖时是非常有用的。它有助于让人们对有关该主题的问题感兴趣的人士前进,从而更迅速地为您解答问题。它还有助于未来的用户为他们的问题寻找解决方案。 –
从谷歌搜索看起来像你是指iOS或OS X框架的工具 - 但问题和标签应该这样说,所以iOS/OS X的人看到它。 – twotwotwo