ios 仅通过 wifi 下载内容或同时使用 wifi 和 3G 网络下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12065442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download content over wifi only or use both wifi and 3G network to download
提问by Dixit
I am implementing a method in iOS app that allow user to download content over WiFi only or use both WiFi and 3G network to download content from web server, my question is how to make switch that can be turn on and off for downloading WiFi or 3G only option? just like apple use it for iTunes Store content on iOS, so if user turn wifi only option on than content will only be downloaded using wifi network.
我正在 iOS 应用程序中实现一种方法,该方法允许用户仅通过 WiFi 下载内容或同时使用 WiFi 和 3G 网络从 Web 服务器下载内容,我的问题是如何制作可以打开和关闭以下载 WiFi 或 3G 的开关唯一的选择?就像苹果将它用于 iOS 上的 iTunes Store 内容一样,因此如果用户打开 wifi only 选项,则内容将仅使用 wifi 网络下载。
Should i be using Reachability class or something else?
我应该使用 Reachability 类还是其他什么?
回答by Nicholas M T Elliott
The Reachability class can help you with this. Initialize it as needed for your connectivity, eg:
Reachability 类可以帮助您解决这个问题。根据您的连接需要对其进行初始化,例如:
Reachability *reachability = [Reachability reachabilityWithHostName:@"www.google.com"];
You can now query the 'currentReachabilityStatus' property to determine if you are connected, and if WiFi is available.
您现在可以查询“currentReachabilityStatus”属性以确定您是否已连接,以及 WiFi 是否可用。
NetworkStatus status = reachability.currentReachabilityStatus;
switch(status)
{
case ReachableViaWiFi:
// There's a wifi connection, go ahead and download
break;
case ReachableViaWWAN:
// There's only a cell connection, so you may or may not want to download
break;
case NotReachable:
// No connection at all! Bad signal, or perhaps airplane mode?
break;
}
Of course it's up to you to handle the states correctly in your application.
当然,正确处理应用程序中的状态取决于您。
回答by Alexey Lysenko
Also, you can change URLSessionConfiguration's
property
var allowsCellularAccess: Bool
此外,您可以更改URLSessionConfiguration's
属性
var allowsCellularAccess: Bool
the property turned to false, disallows session to load data via cellular
该属性变为 false,不允许会话通过蜂窝加载数据