Using Apple's Reachability to check remote server reachability in Swift

前端 未结 4 1634
灰色年华
灰色年华 2020-12-09 04:29

I\'m developing an iOS application written in Swift that communicates with a HTTP server on the local network, and I\'m using Apple\'s Reachability class to determine wether

4条回答
  •  悲&欢浪女
    2020-12-09 05:06

    In swift,

    About what I understood of reachability utility propose by Apple (https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html) or tonymillion (https://github.com/tonymillion/Reachability) which are basically the same is :

    you have 3 possible test :

    • local (can access local network but not internet)
    • extern (can access internet by ip address)
    • dns (can access internet and reach an hostname)

    You can test them respectively by starting a notifier with this :

    let wifiReachability = Reachability. reachabilityForLocalWiFi()
    wifiReachability.startNotifier()
    
    let internetReachability = Reachability.reachabilityForInternetConnection()
    hostReachability.startNotifier()
    
    let hostReachability = Reachability(hostName:"www.apple.com")
    hostReachability.startNotifier()
    

    Which will trigger a notification that you can catch with this method : NSNotificationCenter.defaultCenter().addObserver()

    So to use them you can do something like that :

    create a function in your appDelegate which will instantiate the notifier :

    func startReachabilityTest()
    {
        // Allocate a reachability object to test internet access by hostname
        let reach = Reachability(hostName: "www.apple.com")
    
        // Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
        //reach.reachableOnWWAN = false
    
        reach.startNotifier()
    }
    

    Then you can call it in the didFinishLaunchingWithOptions of your appDelegate :

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.startReachabilityTest();
    }
    

    if want to catch the event in any viewController just add this line to viewDidLoad :

    override func viewDidLoad()
    {
        // Here we set up a NSNotification observer. The Reachability that caused the notification
        // is passed in the object parameter
        NSNotificationCenter.defaultCenter().addObserver(
                self,
                selector: "reachabilityChanged:",
                name: kReachabilityChangedNotification,
                object: nil)
    }
    

    and add this method method to react to the event :

    func reachabilityChanged(notice: NSNotification)
    {
        println("reachability changed")
        let reach = notice.object as? Reachability
        if let remoteHostStatus = reach?.currentReachabilityStatus()
        {
            if remoteHostStatus == NetworkStatus.NotReachable
            {
                println("not reachable")
            }
            else
            {
                println("reachable")
            }
        }
    }
    

    You can also catch the event inside your appDelegate by adding NSNotificationCenter.defaultCenter().addObserver() inside didFinishLaunchingWithOptions and then add reachabilityChanged(notice: NSNotification) in it.

    Ah, also note that you can easily add reachability class to your project with cocoapods by adding this line :

    pod 'Reachability', '~> 3.2'
    

    To your pod file and them after a pod install in command line just add this line to xxx-Bridging-Header.h header file (where xxx is the name of your app) :

    #import 
    

    If you don't have bridging header in your project you can follow this tutorial : http://www.learnswiftonline.com/getting-started/adding-swift-bridging-header/

    No need to add systemConfiguration.framework which is already added by pod dependencies.

    Note : Reachability does not work fine in the simulator

    hope this help!

提交回复
热议问题