简体   繁体   English

我怎么知道现在iOS设备何时连接到互联网?

[英]How do I know when the iOS device is now connected to internet?

I have a question, I am developing an app for people who normally works in a zone that does not have an internet connection, the app will store data (coreData) during the day but when the device is connected to internet, the application must send the stored data to be saved in the cloud. 我有一个问题,我正在为通常在没有Internet连接的区域中工作的人们开发一个应用程序,该应用程序将在白天存储数据(coreData),但是当设备连接到Internet时,该应用程序必须发送存储的数据要保存在云中。 Can I know when the device change from not connected to connected? 我可以知道设备何时从未连接更改为已连接吗? Can I launch my app when that happen? 发生这种情况时,我可以启动我的应用程序吗? Can my app wait for events in the background? 我的应用程序可以在后台等待事件吗? Any clues? 有什么线索吗? Anything like a service in Android? 像Android中的服务一样吗? I think about how WhatsApp works, I mean, when I am not connected, I do not receive any messages but when I am connected (even if the app is not running), automatically the app start and I receive all my messages. 我考虑的是WhatsApp的工作原理,我的意思是,当我未连接时,我没有收到任何消息,但是当我连接时(即使该应用程序未运行),该应用程序会自动启动,并且收到所有消息。 Thank you and Hello from Bogotá! 谢谢,您好,波哥大!

To check for an internet connection on an IOS device using swift, use this code below... 要使用swift检查IOS设备上的互联网连接,请使用以下代码...

import Foundation
import SystemConfiguration

public class Reachability {

    class func isConnectedToNetwork() -> Bool {

        var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
        zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
        zeroAddress.sin_family = sa_family_t(AF_INET)

        let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
            SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()
        }

        var flags: SCNetworkReachabilityFlags = 0
        if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
            return false
        }

        let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
        let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0

        return (isReachable && !needsConnection) ? true : false
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户的设备连接到互联网时,如何在iOS上运行方法? - How to run method on iOS when the user's device is connected to the internet? 如何在未连接到互联网时显示警报? iOS版 - How do you display a Alert when not connected to the internet ? iOS 如何知道iOS中是否连接了蓝牙设备? - How to know if a Bluetooth Device is Connected in iOS? 如何在设备连接到网络(wifi / 3G)时检查互联网连接,但在使用Swift的iOS中没有互联网访问? - How to check for internet connectivity when device is connected to a network (wifi/3G) but there is no internet access in iOS using Swift? 如何知道当应用程序在后台且其在ios系统中首先连接时何时检索连接的ble设备? - How to know when to retrieve connected ble device while app is in background and its connected first in ios system? iOS:我如何知道我是否仍连接到操作者的蜂窝网络 - iOS: how do I know if I'm still connected to oprators's cellular network CBPeripheralManager如何知道何时连接到中央设备? - How can a CBPeripheralManager know when it is connected to a central device? 连接到wifi但没有intenernet时如何检测互联网连接:iOS - How to detect internet connection when connected to wifi but no intenernet: iOS 如何知道iOS设备何时插入? - How to know when iOS device is plugged in? 当 iOS 设备连接到 Airpods 时如何播放音频 swift ios - How to play Audio when iOS device connected to Airpods swift ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM