我如何知道我的iOS应用程序的蜂窝访问是否被禁用?

时间:2020-12-11 07:10:42

I have an iOS app that makes some small network requests on app launch (resource updates, etc). If the user turns off cellular access for the app in iOS Settings, they get a prompt from iOS about network usage every time they launch. Is there a way to know programmatically that cellular data for this app has been disabled, so that I can disable the requests at startup?

我有一个iOS应用程序,可以在应用程序启动(资源更新等)上发出一些小的网络请求。如果用户在iOS设置中关闭应用程序的蜂窝访问权限,则每次启动时都会从iOS获取有关网络使用情况的提示。有没有办法以编程方式知道此应用程序的蜂窝数据已被禁用,以便我可以在启动时禁用请求?

5 个解决方案

#1


13  

So I found this on the apple dev forums from an Apple engineer (https://devforums.apple.com/message/1059332#1059332).

所以我在Apple工程师的苹果开发论坛上找到了这个(https://devforums.apple.com/message/1059332#1059332)。

Another developer wrote in to DTS and thus I had a chance to investigate this in depth. Alas, the news is much as I expected: there is no supported way to detect that your app is in this state. Nor is there a way to make a "no user interaction" network connection, that is, request that the connection fail rather than present UI like this. If these limitations are causing problems for your app, I encourage you to file a bug describing your specific requirements.
https://developer.apple.com/bug-reporting/

另一位开发人员写信给DTS,因此我有机会对此深入研究。唉,这个消息和我预期的一样:没有支持的方法来检测你的应用是否处于这种状态。也没有办法建立“无用户交互”的网络连接,即请求连接失败而不是像这样呈现UI。如果这些限制导致您的应用出现问题,我建议您提交一份描述您具体要求的错误。 https://developer.apple.com/bug-reporting/

So it looks like it is not possible to detect if cellular data for your app has been turned off.

因此,似乎无法检测您的应用的移动数据是否已关闭。

Edit

I filed a radar for this requesting that it be added. I just got this notification in my radar

我为此提出了一个雷达,要求添加它。我刚收到这个通知

We believe this issue has been addressed in the latest iOS 9 beta.

我们认为此问题已在最新的iOS 9测试版中得到解决。

I looked through the API diffs, but so far I can't find the new API.

我查看了API差异,但到目前为止我找不到新的API。

#2


5  

As of iOS9, the capability to check the setting to enable/disable use of cellular data for your app (Settings/Cellular/AppName) is available using Apple's CTCellularData class. The following code will set cellularDataRestrictedState when it is run initially and then set it and log whenever it changes:

从iOS9开始,可以使用Apple的CTCellularData类检查设置以启用/禁用应用程序的蜂窝数据(Settings / Cellular / AppName)。以下代码将在最初运行时设置cellularDataRestrictedState,然后设置它并在其发生更改时进行记录:

import CoreTelephony
var cellularDataRestrictedState = CTCellularDataRestrictedState.restrictedStateUnknown
let cellState = CTCellularData.init()
cellState.cellularDataRestrictionDidUpdateNotifier = { (dataRestrictedState) in
  if cellularDataRestrictedState != .restrictedStateUnknown { // State has changed - log to console
    print("cellularDataRestrictedState: " + "\(dataRestrictedState == .restrictedStateUnknown ? "unknown" : dataRestrictedState == .restricted ? "restricted" : "not restricted")")
  }
  cellularDataRestrictedState = dataRestrictedState
}

Unfortunately (as of iOS11) this seems to check only the state of the app's switch - if your app's switch is set to enabled and the user switches the Cellular Data master switch to disabled, this API will return the app's state as being "not restricted".

不幸的是(从iOS11开始)这似乎只检查应用程序切换的状态 - 如果您的应用程序的开关设置为启用且用户将蜂窝数据主切换器切换为禁用,则此API将使应用程序的状态返回为“不受限制” ”。

#3


2  

Just wanted to add an Objective C version of the above Swift code for future travellers.

只是想为未来的旅行者添加上述Swift代码的Objective C版本。

- (void)monitorCanUseCellularData {
    if (GCIsiOS9) {
        CTCellularData *cellularData = [[CTCellularData alloc] init];
        NSLog(@"%ld", cellularData.restrictedState);
        // 0, kCTCellularDataRestrictedStateUnknown
        [cellularData setCellularDataRestrictionDidUpdateNotifier:^(CTCellularDataRestrictedState state) {
            NSLog(@"%ld", state);
            self.canUseCellularData = cellularData.restrictedState ==2?true:false;
        }];
    }
}

#4


-1  

Adding to dirkgroten's answer, you can use the Apple Reachability class, found here:

添加到dirkgroten的答案,您可以使用Apple Reachability类,可在此处找到:

https://developer.apple.com/Library/ios/samplecode/Reachability/Introduction/Intro.html

It uses SCNetworkReachability, and is very straight forward to use, it will detect connectivity via Cell and WiFi as you will need to check both at start up.

它使用SCNetworkReachability,并且非常直接使用,它将通过Cell和WiFi检测连接,因为您需要在启动时检查两者。

#5


-2  

There are lots of frameworks out there that will give you the status of your network connectivity, and of course you can roll your own. I've found AFNetworking to be one of the best. It has a singleton class called AFNetworkReachabilityManager that abstracts some of the complexities for you. Specifically you'll want to look at the two boolean properties:

有很多框架可以为您提供网络连接的状态,当然您可以自己动手。我发现AFNetworking是最好的之一。它有一个名为AFNetworkReachabilityManager的单例类,可以为您提取一些复杂性。具体来说,您将要查看两个布尔属性:

reachableViaWWAN
reachableViaWiFi 

There is also a reachability changed status block that you can set:

您还可以设置可更改性状态块:

– setReachabilityStatusChangeBlock:

AFNetworking Github

AFNetworkReachabilityManager

#1


13  

So I found this on the apple dev forums from an Apple engineer (https://devforums.apple.com/message/1059332#1059332).

所以我在Apple工程师的苹果开发论坛上找到了这个(https://devforums.apple.com/message/1059332#1059332)。

Another developer wrote in to DTS and thus I had a chance to investigate this in depth. Alas, the news is much as I expected: there is no supported way to detect that your app is in this state. Nor is there a way to make a "no user interaction" network connection, that is, request that the connection fail rather than present UI like this. If these limitations are causing problems for your app, I encourage you to file a bug describing your specific requirements.
https://developer.apple.com/bug-reporting/

另一位开发人员写信给DTS,因此我有机会对此深入研究。唉,这个消息和我预期的一样:没有支持的方法来检测你的应用是否处于这种状态。也没有办法建立“无用户交互”的网络连接,即请求连接失败而不是像这样呈现UI。如果这些限制导致您的应用出现问题,我建议您提交一份描述您具体要求的错误。 https://developer.apple.com/bug-reporting/

So it looks like it is not possible to detect if cellular data for your app has been turned off.

因此,似乎无法检测您的应用的移动数据是否已关闭。

Edit

I filed a radar for this requesting that it be added. I just got this notification in my radar

我为此提出了一个雷达,要求添加它。我刚收到这个通知

We believe this issue has been addressed in the latest iOS 9 beta.

我们认为此问题已在最新的iOS 9测试版中得到解决。

I looked through the API diffs, but so far I can't find the new API.

我查看了API差异,但到目前为止我找不到新的API。

#2


5  

As of iOS9, the capability to check the setting to enable/disable use of cellular data for your app (Settings/Cellular/AppName) is available using Apple's CTCellularData class. The following code will set cellularDataRestrictedState when it is run initially and then set it and log whenever it changes:

从iOS9开始,可以使用Apple的CTCellularData类检查设置以启用/禁用应用程序的蜂窝数据(Settings / Cellular / AppName)。以下代码将在最初运行时设置cellularDataRestrictedState,然后设置它并在其发生更改时进行记录:

import CoreTelephony
var cellularDataRestrictedState = CTCellularDataRestrictedState.restrictedStateUnknown
let cellState = CTCellularData.init()
cellState.cellularDataRestrictionDidUpdateNotifier = { (dataRestrictedState) in
  if cellularDataRestrictedState != .restrictedStateUnknown { // State has changed - log to console
    print("cellularDataRestrictedState: " + "\(dataRestrictedState == .restrictedStateUnknown ? "unknown" : dataRestrictedState == .restricted ? "restricted" : "not restricted")")
  }
  cellularDataRestrictedState = dataRestrictedState
}

Unfortunately (as of iOS11) this seems to check only the state of the app's switch - if your app's switch is set to enabled and the user switches the Cellular Data master switch to disabled, this API will return the app's state as being "not restricted".

不幸的是(从iOS11开始)这似乎只检查应用程序切换的状态 - 如果您的应用程序的开关设置为启用且用户将蜂窝数据主切换器切换为禁用,则此API将使应用程序的状态返回为“不受限制” ”。

#3


2  

Just wanted to add an Objective C version of the above Swift code for future travellers.

只是想为未来的旅行者添加上述Swift代码的Objective C版本。

- (void)monitorCanUseCellularData {
    if (GCIsiOS9) {
        CTCellularData *cellularData = [[CTCellularData alloc] init];
        NSLog(@"%ld", cellularData.restrictedState);
        // 0, kCTCellularDataRestrictedStateUnknown
        [cellularData setCellularDataRestrictionDidUpdateNotifier:^(CTCellularDataRestrictedState state) {
            NSLog(@"%ld", state);
            self.canUseCellularData = cellularData.restrictedState ==2?true:false;
        }];
    }
}

#4


-1  

Adding to dirkgroten's answer, you can use the Apple Reachability class, found here:

添加到dirkgroten的答案,您可以使用Apple Reachability类,可在此处找到:

https://developer.apple.com/Library/ios/samplecode/Reachability/Introduction/Intro.html

It uses SCNetworkReachability, and is very straight forward to use, it will detect connectivity via Cell and WiFi as you will need to check both at start up.

它使用SCNetworkReachability,并且非常直接使用,它将通过Cell和WiFi检测连接,因为您需要在启动时检查两者。

#5


-2  

There are lots of frameworks out there that will give you the status of your network connectivity, and of course you can roll your own. I've found AFNetworking to be one of the best. It has a singleton class called AFNetworkReachabilityManager that abstracts some of the complexities for you. Specifically you'll want to look at the two boolean properties:

有很多框架可以为您提供网络连接的状态,当然您可以自己动手。我发现AFNetworking是最好的之一。它有一个名为AFNetworkReachabilityManager的单例类,可以为您提取一些复杂性。具体来说,您将要查看两个布尔属性:

reachableViaWWAN
reachableViaWiFi 

There is also a reachability changed status block that you can set:

您还可以设置可更改性状态块:

– setReachabilityStatusChangeBlock:

AFNetworking Github

AFNetworkReachabilityManager