如何强制iOS应用程序使用3G而不是WiFi? [英] How do I force an iOS app to use 3G instead of WiFi?

查看:271
本文介绍了如何强制iOS应用程序使用3G而不是WiFi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一种在3G上运行良好的消息传递协议,但不能在某些企业防火墙上使用。即使在可以访问WiFi网络的情况下,我的应用程序如何在无法通过WiFi连接时强制使用蜂窝网络?

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?

编辑:在阅读了Reachability类的实现之后,我仍然不确定这两者是否确实是互斥的。很可能通过 gethostbyname()发现这两个接口,我可能会试着看看它是什么。

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.

推荐答案

使用getifaddrs枚举网络接口。查找以pdp开头的ifa_name。这将是蜂窝接口(例如,pdp_ip0)。测试(ifa_flags& IFF_UP)以确保接口已启动,并从ifa_addr获取IP地址。然后使用bind()将套接字绑定到该地址。

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.

如果蜂窝接口尚未启动,则不确定如何激活它。通常情况下,我只是进行一个高级别的http呼叫,让iOS唤醒网络,但不确定在什么条件下,当wifi也可用时,蜂窝服务变为活动状态。我怀疑它通常会作为无线网络的后备。

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.

这篇关于如何强制iOS应用程序使用3G而不是WiFi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆