【问题标题】:How to automatically recognise internet connection using Swift 3如何使用 Swift 3 自动识别互联网连接
【发布时间】:2016-12-07 06:12:45
【问题描述】:

每当数据保存到手机上的本地数据库 (sqlite) 时,我想与我的 App-Server 同步数据。我需要一种自动检测互联网连接的机制,以便随时将相关保存的数据发送到我的服务器。 这就是我想实现离线模式的方式。

您可以向我推荐哪些方法?是否有任何框架可以完成这项工作?在android中我使用syncAdapter,Swift3是否提供类似的框架还是我需要创建自己的解决方案?

【问题讨论】:

标签: ios swift3 offline data-synchronization offline-mode


【解决方案1】:

步骤-

  1. 在您的 pod 文件中使用 pod 'ReachabilitySwift'
  2. 在“AppDelegate”中导入 ReachabilitySwift
  3. 在 AppDelegate 中编写以下代码

     var reachabilityManager:ReachabilityManager = ReachabilityManager.sharedInstance
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        NetworkActivityIndicatorManager.shared.isEnabled = true
        reachabilityManager.initRechabilityMonitor()
        return true
    }
    
    func reachabilityStatusChangeHandler(_ reachability: Reachability) {
        if reachability.isReachable {
            print("isReachable")
        } else {
            print("notReachable")
        }
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    相关资源
    最近更新 更多