admin管理员组

文章数量:1353228

I have written an App which extracts data, over WiFi, from an instrument that creates its own WiFi Hotspot. The instrument provides no internet connection. The iPad version of this App is connects fine as there is no Cellular connection. To get the iPhone version of the App to connect, I have to disable Cellular. Disabling "WiFi Assist" within Cellular settings was not enough to prevent iOS from trying to route a connection over the cellular network. Is there not a programmatic way to stop iOS from looking for a connection supporting the internet. Apple is rather vague on this topic.

The following code doesn't force a WiFi only connection unless Cellular is disabled.

    let host = NWEndpoint.Host( ipAddress )
    let port = NWEndpoint.Port( portNumber )
    let endpoint = NWEndpoint.hostPort(host: host, port: port!)
    let msgToUser = "Attempting connection host:\(ipAddress) port:\(UInt16(portNumber)!)"
    sessionDelegate!.feedback( with: msgToUser)
    
    // Force Wi-Fi-only connectivity
    let parameters = NWParameters.tcp
    // Attempt to explicitly set the Wi-Fi interface
    if let wifiInterface = NWPathMonitor().currentPath.availableInterfaces.first(where: { $0.type == .wifi }) {
        parameters.requiredInterface = wifiInterface
    } else {
        print("⚠️ No available Wi-Fi interface found")
    }
    parameters.requiredInterfaceType = .wifi
    
    connection = NWConnection(to: endpoint, using: parameters)

本文标签: iosiPhone WiFi connection to HotSpot not connected to internet failsStack Overflow