The Reachability library that bloggers use to import Pods is, of course, the Object-C library. If you don’t have Pods, please check the installation method in my blog Xcode tool category.
Please check the method of creating and using bridge files in my blog Swift classification and then dump them into Reachability.
All done, create an OC file, inherit with NSObject, write:
H medium + (BOOL) isHaveNetwork; (BOOL) isHaveNetwork {//Reachability. H is not a file written by ASI, but a file in apple's official documentation, which is used to determine the network. All are simple judgment network, and there are errors or old version // simple judgment network how to determine the Name of your server address, of course, can also be baidu, mainly used to determine whether your server can be connected, such as server crash, Do not connect the / / Create a zero addy Reachability * re = [Reachability reachabilityWithHostName: @ "www.baidu.com"]. if ([re currentReachabilityStatus] == ReachableViaWiFi || [re currentReachabilityStatus] == ReachableViaWWAN) { return YES; }else{ return NO; }}Copy the code
#import “havenetwork.h” to add the class name to the bridge file.
You can then use this method in swift files:
If HaveNetwork. IsHaveNetwork () {print (" web ")} else {print (" no network ")}}Copy the code
If you want to determine whether it’s Wi-Fi or cellular, you can change the packaging method slightly. Demo download address: github.com/codeliu6572…