Forcing cellular connectivity

I'm trying to force the connectivity over cellular only , without much luck so far, the wifi is always used unless it's turned off

Any idea?

Code Block
var connection: NWConnection?
  func startConnection(url: URL) {
    let pathMonitor = NWPathMonitor()
    pathMonitor.pathUpdateHandler = { path in
      if path.usesInterfaceType(.wifi) {
        print("Path is Wi-Fi")
      } else if path.usesInterfaceType(.cellular) {
        print("Path is Cellular")
      } else if path.usesInterfaceType(.wiredEthernet) {
        print("Path is Wired Ethernet")
      } else if path.usesInterfaceType(.loopback) {
        print("Path is Loopback")
      } else if path.usesInterfaceType(.other) {
        print("Path is other")
      }
    }
    pathMonitor.start(queue: .main)
     
     
    let params = NWParameters.tls
    params.requiredInterfaceType = .cellular
    params.prohibitExpensivePaths = false
    params.prohibitedInterfaceTypes = [.wifi]
    let connection = NWConnection(host: NWEndpoint.Host(url.host!), port: 443, using: params)
    self.connection?.stateUpdateHandler = { (newState) in
      print("This is stateUpdateHandler:")
      switch (newState) {
        case .ready:
          print("State: Ready\n")
        case .setup:
          print("State: Setup\n")
        case .cancelled:
          print("State: Cancelled\n")
        case .preparing:
          print("State: Preparing\n")
        default:
          print("ERROR! State not defined!\n")
      }
    }
     
    connection.start(queue: .main)
    self.connection = connection
    print(connection.debugDescription)
  }

Using the following would be the correct approach to force the connection over cellular:

Code Block swift
let tcpOptions = NWProtocolTCP.Options()
let params = NWParameters(tls: .init(), tcp: tcpOptions)
params.requiredInterfaceType = .cellular
params.prohibitExpensivePaths = false
params.prohibitedInterfaceTypes = [.wifi]
connection = NWConnection(host: "apple.com", port: 443, using: params)


After the connection moves into the .ready state I can see the connection's description being displayed as:
Code Block
[C1 connected apple.com:443 tcp, tls, indefinite, path satisfied (Path is satisfied), interface: pdp_ip0, scoped, ipv4, ipv6, dns, expensive]

So the connection is setup on pdp_ip0 (cell) and is satisfied.

What are you seeing at this point?


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Thanks !

Does this solution still work and is it still the preferred method to forcing communication over cellular, even if connected to WI-FI?

Forcing cellular connectivity
 
 
Q