First up, terminology. I’m not entirely sure what you mean by “native api” but I suspect you’re referring to BSD Sockets, that is,
socket
,
bind
,
connect
, and so on.
If so, you’re use of the term “native api” suggest a serious misunderstanding of the iOS network architecture. The Network framework is not a wrapper around the ‘native’ BSD Sockets API [1]. Rather, iOS has two networking stacks, one inside the kernel and one running in user space. BSD Sockets runs through the in-kernel stack and Network framework runs through the user space one. In most cases the user space stack is faster, and that’s one of the reasons we recommend that folks use the Network framework.
So, it’s fine to use BSD Sockets on iOS — that API is super popular and it’s hard to imagine it going away — but it’s not more ‘native’ than Network framework.
Finally, there are some gotchas associated with using BSD Sockets. See this post for details.
With that out of the way, let’s return to your actual question (-: You wrote:
Is there any way in [BSD Sockets] where we can block the WiFi interface and transfer all traffic through LTE?
BSD Sockets has no direct equivalent to Network frameworks’
prohibitedInterfaceTypes
property. The traditional approach in BSD Sockets is to bind the socket to a specific interface. iOS supports
scoped routing, so if a connection is bound to a specific interface it will always run over that interface.
Bind to an interface using
bind
and the interface’s address, but it’s generally easier to use
IP_BOUND_IF
.
Identify the WWAN interface using
getifaddrs
. Specifically, the interface’s interface type will be
IFT_CELLULAR
.
IMPORTANT One thing to watch out for here is that iOS can have multiple WWAN interfaces and there’s no supported way to determine which is the correct one to use for general WWAN traffic. Probably your best option here is to run a test connection using Network framework and then get the interface name from that.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
[1] On iOS. On macOS it currently is, and is likely to remain so until we stop supporting NKEs.