问题描述
有没有一种方法可以通过编程方式访问蜂窝数据"?有时会显示在应用程序中的选项吗?
Is there a way to programmatically access the "Cellular Data" options that are sometimes shown in apps?
我有一个通过Internet上传数据文件的应用程序.我为用户提供了在不连接WiFi时禁用此上传功能的选项.
I have an application that uploads data files over the internet. I have given users the option of disabling this upload functionality when not connected to WiFi.
我目前正在做的是写了一个settings.bundle
,它允许用户在我的代码中选择一个名为 User Cellular Data 的选项.我正在使用(c href ="https://github.com/ashleymills/Reachability.swift" rel ="nofollow noreferrer"> https://github.com/ashleymills/Reachability.swift )-这是围绕可达性iOS API-基本上可以告诉您是否有Wifi或蜂窝网络访问.
What I'm currently doing is I wrote a settings.bundle
that allows the user to select an option called User Cellular Data in my code I am using a Reachability.swift
file from (https://github.com/ashleymills/Reachability.swift) - which is a swift wrapper around the reachability iOS api - basically it can tell you if there is Wifi or cellular network access.
在执行上传操作之前,我在代码中检查了NSUserDefaults值(由settings.bundle
设置)
In my code before doing uploads I check a NSUserDefaults value (as set by the settings.bundle
)
let uploadOverCellular = NSUserDefaults.standardUserDefaults()
.boolForKey("allow_upload_over_cellular")
如果这是真的,并且我具有Wifi或蜂窝网络,则允许上传继续进行.如果情况并非如此,并且我具有蜂窝网络,我将停止上传.
If this is true and I have either Wifi or Cellular internet I allow the upload to proceed. If this is not true and I have cellular internet I stop the upload.
所以这很好,然后突然我在查看应用程序的设置,发现我同时具有系统级蜂窝数据选项和我的自定义使用移动数据选项.出现
So this is working nicely and then suddenly I was looking in the settings for my app and I noticed that I have both the System level Cellular Data option as well as my custom Use Cellular Data option. showing up
显然,这可能给用户带来一些困惑-我想知道是否可以通过API访问此系统级别的 Cellualr Data 选项-或通过其他方式进行控制以编程方式设置此设置-我怀疑没有,但是我希望有人能证明我做错了.
Obviously this could cause some confusion for users - I wanted to know if there Is there API access to this system level Cellualr Data option - or some other way to control this setting programmatically - I suspect there is not but I'm hoping somebody can prove me wrong.
推荐答案
我不知道是否可以访问全局蜂窝数据设置,但是如果使用NSURLSession
API下载数据,则可以创建和allowsCellularAccess = false
.
I don't know if there's access to the global cellular data setting, but if you use NSURLSession
APIs for downloading data, you can create an NSURLSessionConfiguration
with allowsCellularAccess = false
.
这篇关于iOS程序化蜂窝数据控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!