Reachability Guide for iOS
I have implemented Reachability like this.
Download https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html and add Reachability.h and .m to your project. Add the SystemConfiguration framework to your project. #import "Reachability.h" where you want to use it. Use this code.
-(BOOL)reachable {
Reachability *r = [Reachability reachabilityWithHostName:@"enbr.co.cc"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if(internetStatus == NotReachable) {
return NO;
}
return YES;
}
When you want to check for reachability...
if ([self reachable]) {
NSLog(@"Reachable");
}
else {
NSLog(@"Not Reachable");
}
Here is the example project that I made. http://dl.dropbox.com/u/3656129/ReachabilityExample.zip
iOS Reachability SCNetworkReachabilitySetCallback not call back when switch form wifi to anthor wifi
Thank you for asking great qestion.
Mainly Reachability
class is use for tracking of internet connectivity changes. There is no provsion for detect wifi changed notification. So in one word Reachability
class not provide this type of funtionality. If you want to achieved your requirement then below customisation required.
You can achived wifi changed notification as below.
When your wifi connection changed or switch to any other network then you have received disconnect state for fraction of second. So
reachabilityChanged
method is called.By using below code, You can get wifi information such as BSSID,SSID(Name) and SSID data. Just store wifi info into local.
- When network changed then you can check new wifi information is matched with previos one or not. If not then user connect with new wifi.
Put below code into reachabilityChanged
method.
import SystemConfiguration.CaptiveNetwork
func printCurrentWifiInfo() {
if let interface = CNCopySupportedInterfaces() {
for i in 0.. let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interface, i)
let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
if let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString), let interfaceData = unsafeInterfaceData as? [String : AnyObject] {
// connected wifi
print("BSSID: \(String(describing: interfaceData["BSSID"])), SSID: \(String(describing: interfaceData["SSID"])), SSIDDATA: \(String(describing: interfaceData["SSIDDATA"]))")
} else {
//Wifi is not connected
}
}
}
}
Update:
Apple provide com.apple.system.config.network_change
to listen wifi changes notification. Basically it is part of Core Foundation
framework. I am sure it will work for you.
Please add below code to listen wifi changes.
let notificationName = "com.apple.system.config.network_change"
func onNetworkChange(_ name : String) {
if (name == notificationName) {
// Do your stuff
print("Network was changed")
}
}
func registerObserver() {
let observer = UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque())
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), observer,
{ (nc, observer, name, _, _) -> Swift.Void in
if let observer = observer, let name = name {
let instance = Unmanaged.fromOpaque(observer).takeUnretainedValue()
instance.onNetworkChange(name.rawValue as String)
} },
notificationName as CFString, nil, .deliverImmediately)
}
func removeObserver() {
let observer = UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque())
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), observer, nil, nil)
}
Register observer on init and remove on deinit.
Find reference from Here.
Need some help with Reachability (2.0.3ddg)
OK, so after trying a few things out myself, I actually got it to work, by adding one extra line of code:
-(void)setupReachability {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotificationV2 object:nil];
hostReach = [[ReachabilityV2 reachabilityWithHostName:@"www.google.com"] retain];
[hostReach connectionRequired]; // this line was added, and apparently forces a connection requirement..
[hostReach startNotifier];
}
iOS 5.1 Reachability
https://github.com/tonymillion/Reachability
iOS5 / GCD / ARC friendly version
Is Apple's current Reachability class backwards-compatible with iOS 3.1?
Never found an answer, but I implemented Andrew Donoho's version of the class and it worked fine on iOS 3.1.2.
Related Topics
Cordova App Hanging During Startup on iOS 10
Iphone Get a List of All Ssids Without Private Library
Sorting Array Alphabetically With Number
How to Correctly Linearize Depth in Opengl Es in Ios
Remove or Uninstall Library Previously Added: Cocoapods
Uibutton Inside a View That Has a Uitapgesturerecognizer
How to Create a Guid/Uuid Using Ios
Getting a List of Files in a Directory With a Glob
Dismiss Keyboard by Touching Background of Uitableview
Warning :-Presenting View Controllers on Detached View Controllers Is Discouraged
A Swift Example of Custom Views For Data Input (Custom In-App Keyboard)
How to Calculate the Size of a Folder
Create Uiimage With Solid Color in Swift
How to Tell Swiftui Views to Bind to Nested Observableobjects
Uibutton Action in Table View Cell
How to Get the Name of Image Picked Through Photo Library in Iphone