I have the following code to force the connectivity over cellular and it works perfectly
	 let tcpOptions = NWProtocolTCP.Options()
tcpOptions.connectionTimeout = 5
var tlsOptions: NWProtocolTLS.Options?
var port = 80
if (url.scheme!.starts(with:"https")) {
port = 443
tlsOptions = .init()
}
/*force network connection to cellular only*/
let params = NWParameters(tls: tlsOptions , tcp: tcpOptions)
params.requiredInterfaceType = .cellular
params.prohibitExpensivePaths = false
params.prohibitedInterfaceTypes = [.wifi]
/* create network connection */
connection = NWConnection(host: NWEndpoint.Host(url.host!), port: NWEndpoint.Port(rawValue: UInt16(port))!, using: params)
Now I'm looking to force the connection to use ipv4 (even if the phone has an ipv6 available) because the targeted server has a broken ipv6 support and I don't have control over it (it belongs to a mobile operator)
Any idea?
E
Post
Replies
Boosts
Views
Activity
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?
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)
}