3

How to get the SSID (Service Set Identifier), I have been search for few while but nothing useful. Is that anyone can help?

However, I try this code in ios7

-(NSString *)getWifiName{
    NSString *wifiName = @"Not Found";
    CFArrayRef myArray = CNCopySupportedInterfaces();
    if (myArray != nil) {
        CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
        if (myDict != nil) {
            NSDictionary *dict = (NSDictionary*)CFBridgingRelease(myDict);

            wifiName = [dict valueForKey:@"SSID"];
        }
    }
    NSLog(@"wifiName:%@", wifiName);
    return wifiName;
}

but it cant get the SSID.

modusCell
  • 13,151
  • 9
  • 53
  • 80
luziqin
  • 51
  • 1
  • 9

3 Answers3

3

(Tested on Xcode 8 and Swift 3) First you need to add

@import SystemConfiguration.CaptiveNetwork;
#include <SystemConfiguration/SystemConfiguration.h>

Then the objective-c code is

- (NSString *) getSSID {
NSString *wifiName = nil;
NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
for (NSString *ifnam in ifs) {
    NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
    if (info[@"SSID"]) {
        wifiName = info[@"SSID"];
    }
}
return wifiName;}

If you want to use swift, then you need to add the following code to the bridging-header

#include <ifaddrs.h>

The code for swift (swift 3) is

func fetchSSIDInfo() ->  String {
    var currentSSID = ""
    if let interfaces:CFArray = CNCopySupportedInterfaces() {
        for i in 0..<CFArrayGetCount(interfaces){
            let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
            let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
            let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
            if unsafeInterfaceData != nil {

                let interfaceData = unsafeInterfaceData! as Dictionary!
                currentSSID = ((interfaceData as? [String : AnyObject])?["SSID"])! as! String

            }
        }
    }
    return currentSSID
}
Burak Gavas
  • 1,304
  • 1
  • 9
  • 11
1

Try this: (Edited)

- (NSString *)wifiName
{
    NSString *wifiName = @"Not Found";
    CFArrayRef interfaces = CNCopySupportedInterfaces();
    if (interfaces)
    {
        CFDictionaryRef networkDetails = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(interfaces, 0));
        if (networkDetails)
        {
            wifiName = (NSString *)CFDictionaryGetValue(networkDetails, kCNNetworkInfoKeySSID);
            CFRelease(networkDetails);
        }
    }

    return wifiName;
}
Pavlo Shadov
  • 382
  • 4
  • 16
  • Strange, I copy-pasted this code directly from my project and it works pretty fine there. I think that problem is in another place – Pavlo Shadov Aug 12 '14 at 09:32
  • still error and fail by "exc_bad_access" on the sentence “CFDictionaryRef networkDetails = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(interfaces, 0));” – luziqin Aug 12 '14 at 09:39
  • It means that interfaces array is null. I edited the code a bit – Pavlo Shadov Aug 12 '14 at 13:03
  • Here you can read about CNCopySupportedInterfaces function https://developer.apple.com/Library/ios/documentation/SystemConfiguration/Reference/CaptiveNetworkRef/Reference/reference.html#//apple_ref/c/func/CNCopySupportedInterfaces. I think it may be something with your wi-fi options. – Pavlo Shadov Aug 12 '14 at 13:06
  • @PavloShadov it is deprecated in iOS9 – OhadM Feb 17 '16 at 08:24
0
+ (NSString*)SSID
{
    NSArray* ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    id info = nil;
    for (NSString* ifnam in ifs)
    {
        info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
        if (info && [info count])
            break;
    }
    return info[@"SSID"];
}
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51