我正在构建一个有几页的iOS应用程序。我在登录时获取完整的App(对于所有页面)JSON数据,然后将它们存储到CoreData中,以便数据立即可供离线查看。 (只读)
我的问题是如何实现在线/离线查看方案。
任何建议将不胜感激。
我会用THIS Git project来做到这一点:
extension UIViewController {
/**
Check network availability with Ashley Mills' Reachability.
*/
func doReachability(_ reachability: Reachability,_ completion: @escaping (_ connected: Bool?) -> Void) {
reachability.whenReachable = { reachability in
completion(true)
}
reachability.whenUnreachable = { _ in
completion(false)
}
do{
try reachability.startNotifier()
}catch{
print("could not start reachability notifier")
}
}
然后在视图中你需要这样做:
let reachability = Reachability()!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
doReachability(reachability) { (connected) in
if connected == false {
print("No Connection! Your device isn't connected to the network." /* DO ANY WORK HERE */
{ else {/*Connected work here*/}
})
}
}