如何检查移动数据或wifi是打开还是关闭。 ios swift [英] How can i check mobile data or wifi is on or off. ios swift

查看:815
本文介绍了如何检查移动数据或wifi是打开还是关闭。 ios swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在检查如果移动数据已关闭,则会显示弹出窗口,例如检查您的数据连接。因为我写这个代码

in my app i am checking that if mobile data is off its showing the popup like check your data connection. for that i write this code

 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(kCFAllocatorDefault, UnsafePointer($0))
        }

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

        let isReachable = flags == .Reachable
        let needsConnection = flags == .ConnectionRequired

        return isReachable && !needsConnection

    }
}

但是通过此代码只检查wifi是否连接。但如果我尝试使用3g移动数据,它总是向我显示您的移动数据未连接。那我怎么解决这个问题?

but by this code its only check that wifi is connected or not. but if i try with 3g mobile data its always showing me that your mobile data is not connected. so how can i solve this?

推荐答案

尝试下面的代码:

创建可达性类的对象,例如,

Create object of reachability class like,

    var internetReachability = Reachability()

现在在viewDidLoad()中写下面的代码,

Now write below code in viewDidLoad(),

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: nil)
    self.internetReachability = Reachability.reachabilityForInternetConnection()
    self.internetReachability.startNotifier()
    self.updateInterfaceWithReachability(self.internetReachability)

现在创建函数来检查wifi的可达性为以及数据包,

Now create function to check reachabilty of wifi as well as data pack,

func updateInterfaceWithReachability(reachability: Reachability)
{
    let netStatus : NetworkStatus = reachability.currentReachabilityStatus()
    switch (netStatus.rawValue)
    {
    case NotReachable.rawValue:
        print("offline")
        break
    case ReachableViaWWAN.rawValue:
        print("online")
        break
    case ReachableViaWiFi.rawValue:
        print("online")
        break
    default :
        print("offline")
        break
    }
}

// Rechability update status
func reachabilityChanged(sender : NSNotification!)
{
    let curReach : Reachability = sender.object as! Reachability
    self.updateInterfaceWithReachability(curReach)
}

希望这会对你有所帮助。

Hope this will help you.

这篇关于如何检查移动数据或wifi是打开还是关闭。 ios swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆