gpt4 book ai didi

ios - 在 Swift 中使用 PFQueryTableViewController 检查互联网连接

转载 作者:行者123 更新时间:2023-11-28 08:38:38 25 4
gpt4 key购买 nike

我正在开发一个与 Parse 链接的应用程序,因此我使用的是 PFQueryTableViewController

我只是想知道如何在运行该应用程序时检查互联网连接。

目前,如果我关闭互联网连接然后运行该应用程序,我会得到表格的加载微调器,但显然,不会加载任何内容。最终,微调器停止了,我只剩下一张空白表格。

我读过有关使用 Reachability 的信息,但不太确定如何将它放入我当前的应用程序中。

本质上,我想要的是,每次用户启动应用程序时,它都会检查互联网连接,如果有那就太好了!如果没有,它会显示一个提示说没有连接。

有人可以帮忙吗?我已经为 queryForTable() 函数添加了我的代码,这是我认为应该进行检查的地方。如果您需要查看我的任何其他代码,请告诉我。谢谢

override func queryForTable() -> PFQuery {

let query = PFQuery(className: "Reviews")

if indexArray == 0 {

query.orderByDescending("createdAt")

} else if indexArray == 1 {

query.orderByAscending("FilmName")

} else if indexArray == 2 {

query.orderByDescending("OurRating")

} else if indexArray == 3 {

query.orderByAscending("OurRating")

} else if indexArray == 4 {

query.orderByDescending("WantToSeeThisCount")

} else if indexArray == 5 {

query.orderByAscending("DirectedBy")

} else if indexArray == 6 {

query.orderByDescending("UpVoteCount")

} else if indexArray == 7 {

query.orderByDescending("DownVoteCount")

}

query.whereKey("ToDisplayInApp", equalTo:true)

// Add a where clause if there is a search criteria
if filmSearchBar.text != "" {

query.whereKey("FilmName", containsString: filmSearchBar.text!)

}

return query

}

最佳答案

var reachability: Reachability!

func checkNetworkReachability() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(functionToHandleWhenConnectionChanges()), name:kReachabilityChangedNotification, object: nil)

if self.reachability == nil {
self.reachability = Reachability.reachabilityForInternetConnection()
self.reachability.startNotifier()
}
}

func functionToHandleWhenConnectionChanges() {
// self.reachability.isReachable() returns a boolean. If it's yes, then you are connected to internet, otherwise not connected.
}

在执行所有这些操作之前,添加来自 https://github.com/tonymillion/Reachability 的 Reachability.h 和 Reachability.m 文件

希望这有帮助:)

关于ios - 在 Swift 中使用 PFQueryTableViewController 检查互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36923921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com