How do I force an iOS app to use 3G instead of WiFi?
Asked Answered
L

3

5

I want to use a messaging protocol that works fine over 3G, but not over some corporate firewalls. How can my app force the use of the cellular network when it fails to connect over WiFi even in the case that the WiFi network is reachable?

EDIT: After reading through the implementation of the Reachability class I remain unsure whether the two are indeed mutually exclusive. It could well be possible to discover both interfaces via gethostbyname(), which I might try just to see what comes out.

Laparotomy answered 17/10, 2012 at 1:9 Comment(2)
have you found the answer ?Sicken
@entropy: I'm afraid not. I got sidetrack onto other projects before getting a chance to investigate more deeply.Laparotomy
N
6

Use getifaddrs to enumerate the network interfaces. Look for ifa_name that starts with "pdp." This will be the cellular interface (e.g., pdp_ip0). Test (ifa_flags & IFF_UP) to make sure the interface is up, and get the IP address from ifa_addr. Then use bind() to bind your socket to that address.

Not sure how you activate the cellular interface if it is not already up. Normally, I just make a high level http call to get iOS to wake up the network, but not sure under what conditions the cellular service becomes active when wifi is also available. I suspect it is usually there as a fallback to the wifi.

Nonconformist answered 6/1, 2015 at 23:15 Comment(0)
C
2

http://iphonedevsdk.com/discussion/comment/120957

From the looks of that thread, it seems like its impossible. Your best bet may be to try using the protocol, and saying something like "please disconnect from wifi and retry" if it doesn't work.

This thread says just about the same thing:

how to programatically start 3g connection on iphone?

You can use the Reachability class which apple wrote in order to test whether the network connection is currently over 3G or WiFi, and even to check if a specific resource is available on the network.

Codfish answered 17/10, 2012 at 1:24 Comment(1)
Thanks for the links. I've come across several such postings, but I was hoping for something more definitive. See my amended question for my thoughts on the Reachability class.Laparotomy
P
2

You can use any of the socket library to force to use cellular. Find the cellular interface from getifaddrs, pass the interface to the socket function, socket bind will happen with cellular. e.g. CocoaAsyncSocket

Percival answered 17/5, 2018 at 9:33 Comment(1)
hi Kumar, do you know how to bind interface but with IPv6? because when I turn on 4G only, it shows my ip is ipv6. but when I tried to connect via socket, it return ipv4Invocate

© 2022 - 2024 — McMap. All rights reserved.