如何获取设备搜索阶段可用的网络SSID列表及NetworkExtensions实现
Hey there! Let's break this down step by step since you're working with NetworkExtensions to grab WiFi SSID lists, and you're curious about how to use kNEHotspotHelperCommandTypeFilterForScan.
一、核心前提:权限与系统限制
First off, it's important to remember that on iOS/macOS, you can't get WiFi scan results with regular APIs—you need the Hotspot Helper capability from NetworkExtensions, which you're already in the process of applying for. Once Apple approves your entitlement, you can use the code below to pull the SSID list.
二、Using kNEHotspotHelperCommandTypeFilterForScan & Full Code Example
This command type is how you ask the system to either filter existing scan results or trigger a new scan and send you the results. Here's a complete set of custom functions you can drop into your project:
1. Register the Hotspot Helper (Run Early in App Launch)
You need to register the helper as soon as your app starts to listen for system commands:
import NetworkExtension func registerHotspotHelper() { let helperOptions: [String: NSObject] = [ kNEHotspotHelperOptionDisplayName: "Your App's Display Name" as NSObject ] NEHotspotHelper.register(options: helperOptions) { incomingCommand in switch incomingCommand.commandType { case .filterForScan: // Send a request to the system to return all scanned WiFi networks let response = incomingCommand.createResponse(.success) // Pass an empty array to request ALL networks (you can add filters here if needed) response.setNetworkList([]) incomingCommand.deliver(response) case .scanResults: // This is where you'll receive the actual scan results from the system guard let availableNetworks = incomingCommand.networkList else { return } for network in availableNetworks { let ssid = network.ssid ?? "Unknown SSID" let bssid = network.bssid ?? "Unknown BSSID" let signalStrength = network.signalStrength // 0 (weak) to 1 (strong) let isSecure = network.isSecure // true for WPA/WPA2/WPA3 // Do whatever you need with this data—store it, display it, etc. print("Found Network: \(ssid) | Signal: \(signalStrength) | Secure: \(isSecure)") } default: // Handle other command types if needed (like evaluate, authenticate) break } } }
2. Trigger a WiFi Scan
Call this function whenever you want to fetch the latest list of available networks (e.g., when a user taps a "Refresh" button):
func requestLatestWiFiScan() { // Check if the filterForScan command is supported guard NEHotspotHelper.supportedCommands.contains(.filterForScan) else { print("Filter for scan command not supported on this device") return } // Create and send the scan request let scanCommand = NEHotspotHelperCommand.createCommand(type: .filterForScan, parameters: nil) NEHotspotHelper.processCommand(scanCommand) { response in if response.result == .success { print("Scan request sent—waiting for results...") } else { print("Failed to send scan request") } } }
3. Critical Setup Steps
Don't forget these mandatory configurations:
- Add the
com.apple.developer.networking.HotspotHelperentitlement to your app's Info.plist and ensure it's enabled in your Apple Developer account (you mentioned you're already working on this) - Call
registerHotspotHelper()in your app's launch method (likeapplication(_:didFinishLaunchingWithOptions:)for UIKit, or in your app's initializer for SwiftUI) - The system limits how often you can trigger scans, so avoid calling
requestLatestWiFiScan()too frequently (Apple recommends spacing requests by at least a few seconds)
三、Quick Notes
- The scan results are delivered asynchronously, so make sure to update your UI on the main thread when you receive them
- You can filter the network list by passing specific SSIDs to
response.setNetworkList([])(replace the empty array with an array ofNEHotspotNetworkobjects matching your criteria)
内容的提问来源于stack exchange,提问作者Dmytrii G.

