了解 iOS 设备是否具有蜂窝数据功能
我的应用程序中有一个“仅通过 WiFi 下载”的切换开关。然而,该切换对于 iPod touch 或 WiFi-iPad 来说毫无用处。
有没有办法通过代码知道设备是否具有蜂窝数据功能?未来可行的东西也很棒(比如带有 3G 功能的第 5 代 iPod touch 的问世)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您好,您应该能够检查它是否具有 pdp_ip0 接口,
这不使用任何私有 API。
Hi you should be able to check if it has the pdp_ip0 interface
This doesn't use any private APIs.
3G 本身似乎很难找到。您可以使用
[[了解设备是否可以拨打电话 UIApplicationsharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]]
。您可以使用 可达性代码:..但除此之外,我认为您无法检查设备中是否存在 3G 调制解调器。***** 如果可以的话'没有拨打电话,目前也没有打开手机数据并关闭 wifi关闭,您将无法确定它是否支持 3G。
另一种方法(虽然不向前兼容,所以您可能不想这样做)是将设备型号与详尽列表进行比较,了解哪些设备具有 3G 调制解调器,如图所示 这里。
***** 根据 Bentech 的回答,如果您想深入了解设备名称(如果 Apple 决定更改 3g 接口名称,这可能会在没有预先警告的情况下停止工作),请致电
getifaddrs
并检查pdp_ip0
接口。3G by itself seems tough to find. You can find out whether a device can make calls using
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]]
. You can check whether a device can get to the internet, period (and by which method that can currently happen) using Reachability code:..but aside from that, I don't think you can check for the existence of a 3G modem in the device.***** If it can't make a call, and doesn't currently have cell data turned on and wifi turned off, you won't be able to find out if it's 3G-capable.
An alternative way (not forward-compatible though, so you probably don't want to do this) is to compare the device's model with an exhaustive list, knowing which ones have 3G modems in them, as shown here.
***** As per bentech's answer, if you want to go digging around with device names (this may stop working with no advance warning if Apple decide to change the 3g interface name), call
getifaddrs
and check for thepdp_ip0
interface.@bentech 的答案的 Swift 3.0 (UIDevice+Extension)
将此行添加到您的 BridgingHeader.h:
其他地方:
Swift 3.0 (UIDevice+Extension) of @bentech's answer
Add this line to your BridgingHeader.h:
Somewhere else:
在 iOS 6.1 中,我已经能够使用 Core电话成功检查蜂窝基带功能是否存在。这适用于我测试的所有 iPad:Verizon 服务已激活或未激活,AT&T 服务当前已停用,SIM 卡插入和拔出,以及仅支持 Wi-Fi 的 iPad。
我使用的代码如下所示:
对于所有具有蜂窝基带硬件的 iPad,
Carrier
不为零。对于仅支持 Wi-Fi 的 iPad,运营商
为零。In iOS 6.1, I've been able to use Core Telephony to successfully check for the presence of cellular baseband capabilities. This works on all iPads I tested: Verizon with service activated and without, AT&T with service currently deactivated, SIM card in and out, and a Wi-Fi-only iPad.
The code I used looks like this:
For all the iPads with cellular baseband hardware,
carrier
is not nil. For the Wi-Fi-only iPad,carrier
is nil.我认为您应该能够使用 CoreTelephony框架。
它确实指出它是供运营商使用的,所以我不确定访问它是否违反 TOS。
I'd think you should be able to use the CoreTelephony Framework.
It does call out that it is for carriers to use, so I am not sure if it is against TOS to access it.
一种方法是询问用户的位置。当它尽可能准确时,您就会知道设备是否有 GPS。所有具有 GPS 的设备都将具有 3G。那些没有 GPS 的人就没有 3G。
One way of doing it is to ask for the users location. When it is as accurate as possibLe, you will know if the device have GPS. All devices that have GPS will have 3G. And those that don't GPS won't have 3G.
苹果在这里提供了代码。
https://developer.apple.com/library/ios/samplecode /Reachability/Introduction/Intro.html
您应该将 Reachability.h 和 Reachability.m 复制到您的项目中并导入
Reachability.h 到你的班级,然后
Apple provided code here.
https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html
You should copy Reachability.h and Reachability.m to your project and import
Reachability.h to your class,then
另一种方法是扩展它: https://github.com/ monospacecollective/UIDevice-Hardware/blob/master/UIDevice-Hardware.m 如下:
}
(显然,可以对其进行编辑以删除“否”或“是”,仅取决于您想要的方式错误万一有新型号......)
Another way is to extend this: https://github.com/monospacecollective/UIDevice-Hardware/blob/master/UIDevice-Hardware.m with this:
}
(Clearly it could be edited down to remove either the NO or YES only depending on which way you want to err in case there is a new model...)