我如何检查移动数据或无线networking打开或closures。 ios迅速
在我的应用程序,我正在检查,如果移动数据是显示popup式喜欢检查您的数据连接。 为此我写这个代码
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 } }
但通过这个代码它唯一的检查,无线连接与否。 但如果我尝试使用3G移动数据,它始终显示我的移动数据没有连接。 那我该如何解决呢?
尝试像下面的代码:
创build可达性类的对象,
var internetReachability = Reachability()
现在在viewDidLoad()中写下面的代码,
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: nil) self.internetReachability = Reachability.reachabilityForInternetConnection() self.internetReachability.startNotifier() self.updateInterfaceWithReachability(self.internetReachability)
现在创buildfunction来检查wifi的可用性以及数据包,
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) }
希望这会帮助你。
如果您的应用程序中有超级类,请使用下面的代码
override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) NSNotificationCenter.defaultCenter().addObserver(self, selector: "ShowNetConnectivity", name: SHOW_NO_INTERNET_CONNECTION, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: "DisssmissConnectivity", name: DISMISS_INTERNET_CONNECTION, object: nil) let reachability = SCNetworkReachabilityCreateWithName(nil, host)! SCNetworkReachabilitySetCallback(reachability, { (_, flags, _) in print(flags.rawValue) if (flags.rawValue == NotReachable.rawValue && flags.rawValue != ReachableViaWiFi.rawValue && flags.rawValue != ReachableViaWWAN.rawValue) { if(isConnectionAvailable == true) { let nc = NSNotificationCenter.defaultCenter() nc.postNotificationName(SHOW_NO_INTERNET_CONNECTION, object: nil) } }else { if(isConnectionAvailable == false) { let nc = NSNotificationCenter.defaultCenter() nc.postNotificationName(DISMISS_INTERNET_CONNECTION, object: nil) } } }, &context) SCNetworkReachabilityScheduleWithRunLoop(reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes) } override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) NSNotificationCenter.defaultCenter().removeObserver(self, name: SHOW_NO_INTERNET_CONNECTION, object: nil) NSNotificationCenter.defaultCenter().removeObserver(self, name: DISMISS_INTERNET_CONNECTION, object: nil) }
这是我如何做,它适用于我。 在我的viewDidLoad中:
do { reachability = try Reachability.reachabilityForInternetConnection() } catch { print("Unable to create Reachability") return } NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MainViewController.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability) do { try reachability.startNotifier() } catch { print("This is not working.") return }
和可达性改变
func reachabilityChanged(note: NSNotification) { let reachability = note.object as! Reachability if reachability.isReachable() { if reachability.isReachableViaWiFi() { print("Reachable via WiFi") } else { print("Reachable via Cellular") } } else { showNoConnectionAlert() print("Not reachable") } }
使用这个可达性