11

Is there any way to capture the event occurs when a user connects to a particular WiFi network in iOS app. It is fine even if this can be achieved using any private library which doesn't require super user privileges (jail break). I just want to capture the changing event of the connected SSID.

Kara
  • 6,115
  • 16
  • 50
  • 57
Shanaka
  • 1,670
  • 3
  • 21
  • 42
  • 2
    Maybe with that: http://stackoverflow.com/questions/5198716/iphone-get-ssid-without-private-library/7041017#7041017 – Larme Jul 15 '13 at 11:04
  • This is to get the currently connected Wi-Fi SSID. I need to capture the event where user connects to a Wi-Fi network or change the currently connected Wi-FI network to some other. – Shanaka Jul 15 '13 at 11:10

3 Answers3

6

I would recommend simply using what Larme posted, and setting up an NSTimer to check every second or so, what the SSID of your current network is, if you detect a change, simply do whatever you need to do. Keep in mind, changing WiFi networks is not something that happens instantaneously, so having a 1 second resolution is not bad

In applicationDidFinishLoading

NSTimer *ssidTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(fetchSSIDInfo) userInfo:nil repeats:YES];

In AppDelegate

- (id)fetchSSIDInfo {
     NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
     NSLog(@"Supported interfaces: %@", ifs);
     id info = nil;
     NSString *ifnam = @"";
     for (ifnam in ifs) {
         info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
         NSLog(@"%@ => %@", ifnam, info);
         if (info && [info count]) { break; }
     }
     if ([info count] >= 1 && [ifnam caseInsensitiveCompare:prevSSID] !=  NSOrderedSame) {
          // Trigger some event
          prevSSID = ifnam;
     }

     return info;
}

Something like that. I can not check if code is typo free as I am not in front of a mac, but it should not be too different

MZimmerman6
  • 8,445
  • 10
  • 40
  • 70
  • Yes that is how I have done it already. I just wanted to check whether is there any method of doing this. Thanks for the reply – Shanaka Jul 16 '13 at 15:47
  • Not that I know of. There is probably some private method that Apple can use, but if you use it, you will get rejected. But writing your own is not too complicated. – MZimmerman6 Jul 16 '13 at 18:01
  • This is documented https://developer.apple.com/LIBRARY/ios/documentation/SystemConfiguration/Reference/CaptiveNetworkRef/index.html#//apple_ref/c/func/CNCopyCurrentNetworkInfo – pronebird Dec 05 '14 at 16:06
  • 1
    What's wrong with using NetworkReachability API instead of timer? I guess Reachability is much more accurate and reliable.. – pronebird Dec 05 '14 at 16:07
  • 2
    @Andy I am not sure if NetworkReachability can tell whether or not a different network SSID was encountered. Plus, the method I posted is what I knew would work at the time. Plus, a one second resolution is still pretty accurate – MZimmerman6 Dec 09 '14 at 14:39
  • `ifnam` is the interface name, not the SSID, so would not change when the Wi-Fi network changes – user102008 Aug 14 '19 at 21:37
  • Also, `CNCopyCurrentNetworkInfo` no longer returns SSID/BSSID information on iOS 13+ if the app does not have location permission. Is there any way to get it in that case? – user102008 Aug 14 '19 at 21:38
2

You can fetch details from your wifi connection:

- (NSDictionary *)getConnectionDetails
{
    NSDictionary *connectionDetails = [NSDictionary dictionary];
    CFArrayRef myArray = CNCopySupportedInterfaces();
    if (myArray) {
        CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
        connectionDetails = (__bridge_transfer NSDictionary*)myDict;
    }
    return connectionDetails;
}

And then if check [connectionDetails valueForKey:@"BSSID"] you will get BSSID.

Also please note that you must to import #import <SystemConfiguration/CaptiveNetwork.h>

hbk
  • 10,908
  • 11
  • 91
  • 124
  • `CNCopyCurrentNetworkInfo` no longer returns SSID/BSSID information on iOS 13+ if the app does not have location permission. Is there any way to get it in that case? – user102008 Aug 14 '19 at 21:39
1

You want SystemConfiguration, which has facilities for seeing notifications on all sorts of networking changes. In particular you'll want to use SCDynamicStoreSetNotificationKeys to listen for changes to the devices and SCNetworkConfiguration to get information about the available interfaces.

alfwatt
  • 2,010
  • 2
  • 18
  • 29
  • I couldn't even find documentation on what you call "notifications on all sorts of networking changes". If you do could you please point out the documentation on how to detect the wi-fi change? – Mijail Jul 01 '19 at 09:57