How to get available all wifi network name Listing in iOS using swift

微笑、不失礼 提交于 2019-12-22 08:22:14

问题


I have simple question but i dont know how to implement this. I need to create an app that can get all list of available wifi networks names and information when user click on some network iphone have to connect to this network. Can i do this? and How ?


回答1:


It is not possible to get all the available wifi networks names and information. BUT it is only possible to get the SSID (SSID is simply the technical term for a network name) of the network that you are currently connected to.

This class will show only the wifi network name you are connected to -

    import UIKit
    import SystemConfiguration.CaptiveNetwork

    class ViewController: UIViewController {

        @IBOutlet weak var label: UILabel!

        override func viewDidLoad(){
            super.viewDidLoad()
            let ssid = self.getAllWiFiNameList()
            print("SSID: \(ssid)")
        }
        func getAllWiFiNameList() -> String? {
            var ssid: String?
            if let interfaces = CNCopySupportedInterfaces() as NSArray? {
            for interface in interfaces {
            if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
                        ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
                        break
                    }
                }
            }
            return ssid
        }
    }

OUTPUT- (Network name where i am connected to )

To test this you need a physical device (iPhone) connected to your pc.




回答2:


By doing some research I found that:
It is NOT possible in iOS to scan all nearby SSID. We can only get currently connected wifi SSID. If we somehow do this with any private library, App will be rejected by Apple. Some helpful Links I want to share -

  • https://forums.developer.apple.com/thread/39204
  • iOS 11.0 - Periodically scan for SSIDs (WiFi) nearby

You can get connected wifi SSID by these methods:
Swift (3 and 4) -

import SystemConfiguration.CaptiveNetwork

func fetchSSIDInfo() -> String? {
        var ssid: String?
        if let interfaces = CNCopySupportedInterfaces() as NSArray? {
            for interface in interfaces {
                if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
                    ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
                    break
                }
            }
        }
        return ssid
    }

Objective-C -

#import <SystemConfiguration/CaptiveNetwork.h>

+(NSString*)fetchSSIDInfo {

    NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();

    NSDictionary *info;

    for (NSString *ifnam in ifs) {

        info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);

        if (info && [info count]) {

            return [info objectForKey:@"SSID"];
            break;
        }
    }

    return @"No WiFi Available";
}



回答3:


Yes it is possible.You need to complete a questionnaire at https://developer.apple.com/contact/network-extension, and then you can use NEHotspotHelper to return a list of hotspots.



来源:https://stackoverflow.com/questions/49525912/how-to-get-available-all-wifi-network-name-listing-in-ios-using-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!