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

标签 ios swift parse-platform reachability pfquerytableviewcontrolle

我正在开发一个与 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/

相关文章:

ios - Realm 复制所有对象

ios - 为 iOS 应用程序生成 youtube 播放列表 rss 提要的最佳方法是什么?

swift - NVActivityIndi​​catorView 启动时如何返回?

ios - tableview 不显示数据,除非用户触摸

android - 执行 Parse.initialize 时出现 java.util.ConcurrentModificationException(这

iphone - NSSortDescriptor,获取按日期排序的最新 5 条记录

c++ - 为什么 Apple 的 Clang(来自 Xcode 5)为 arm64 制作 typeinfos private_extern?

json - fatal error : Index out of range (while trying to load an image to a table view)

java - 如何让 Parse.com 推送通知在 Cordova/Phonegap Android 应用程序中工作?

iOS 9 Swift 2.0 如何获得通知以便当用户向另一个用户发送消息时他们会收到通知?