1

I need to get notified when connectivity changes during app usage. I have imported Reachability.h .m files to my Swift project, and bridged it by adding #include "Reachability.h".

In Apple's Reachability example project an observer is set up to get notified when connectivity changes. How can I set it up in Swift?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

There is no equivalent observer in Swift. Swift has property observers like willSet and didSet.

János
  • 32,867
  • 38
  • 193
  • 353

1 Answers1

2

Please referred following link for Reachability in swift https://github.com/ashleymills/Reachability.swift

For your add observer please use it like this

NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
  • how have you fixed `localWifiAddress.sin_addr.s_addr = in_addr_t(Int64(0xA9FE0000).bigEndian)` Integer literal overflows when stored into 'Int' issue? in Xcode 6.3? – János Apr 13 '15 at 12:20
  • may this link help you. http://stackoverflow.com/questions/24645798/does-swift-support-implicit-conversion – Chirag Shah Apr 13 '15 at 12:25
  • 3
    There was an update on https://github.com/ashleymills/Reachability.swift, it helped me :) – János Apr 13 '15 at 12:32