2016-12-07 127 views
-3

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

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

+1

'SCNetworkReachability'可以用来检测网络连接的状态和变化tivity。 https://github.com/ashleymills/Reachability.swift是一个众所周知的Swift包装器。您还可以在SO上找到关于SCNetworkReachability的许多问答。 –

+0

[iOS上的同步适配器 - 我的应用程序与我的服务器同步联系人]的可能重复(http://stackoverflow.com/questions/22981812/sync-adapter-on-ios-my-app-sync-contact-with-my -server) – Fay007

+0

[在iOS上创建等价的Androids同步适配器]可能的重复(http://stackoverflow.com/questions/8406736/create-equivalent-of-androids-sync-adapter-on-ios) –

回答

1

步骤做它 -

  1. 使用吊舱在POD文件
  2. 在 'AppDelegate中' 进口 'ReachabilitySwift' 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") 
        } 
    }