【问题标题】:How to get the SSID that the IOS device currently linked如何获取IOS设备当前链接的SSID
【发布时间】:2014-10-05 05:41:50
【问题描述】:

如何获取SSID(服务集标识符),我已经搜索了一段时间,但没有任何用处。有人可以帮忙吗?

但是,我在 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;
}

但它无法获取 SSID。

【问题讨论】:

    标签: ios objective-c wifi ssid


    【解决方案1】:

    (在 Xcode 8 和 Swift 3 上测试) 首先你需要添加

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

    那么objective-c代码就是

    - (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;}
    

    如果要使用swift,则需要在bridging-header中添加如下代码

    #include <ifaddrs.h>
    

    swift (swift 3) 的代码是

    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
    }
    

    【讨论】:

      【解决方案2】:

      试试这个: (已编辑

      - (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;
      }
      

      【讨论】:

      • 奇怪,我直接从我的项目中复制粘贴了这段代码,它在那里工作得很好。我认为问题出在另一个地方
      • 仍然错误并在“CFDictionaryRef networkDetails = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(interfaces, 0));”语句中通过“exc_bad_access”失败
      • 表示interfaces数组为空。我稍微编辑了代码
      • 在这里你可以阅读到 CNCopySupportedInterfaces 函数developer.apple.com/Library/ios/documentation/…。我认为这可能与您的 Wi-Fi 选项有关。
      • @PavloShadov 在 iOS9 中已弃用
      【解决方案3】:
      + (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"];
      }
      

      【讨论】:

        猜你喜欢
        • 2022-06-23
        • 2020-10-16
        • 1970-01-01
        • 2021-02-16
        • 2011-10-13
        • 1970-01-01
        • 2020-01-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多