How do I know if cellular access for my iOS app is disabled?

不想你离开。 提交于 2019-11-27 01:04:19

So I found this on the apple dev forums from an Apple engineer (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/

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.

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

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:

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".

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

- (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;
        }];
    }
}
cescofry

I have found that the CTCellularData class needs some time to get to the correct value. In my implementation I call the didUpdateNotifier very early after appDidFinishLaunching. By the time my networking call are returning with errors I definitely have a correct value for the restricted state.

class CellularRestriction: NSObject {
    private static var cellularData = CTCellularData()
    private static var currentState = CTCellularDataRestrictedState.restrictedStateUnknown

    static var isRestricted: Bool {
        currentState = cellularData.restrictedState
        return currentState == .restricted
    }

    static func prepare() {
        if currentState == .restrictedStateUnknown {
            cellularData.cellularDataRestrictionDidUpdateNotifier = { state in
                currentState = cellularData.restrictedState // This value may be inconsistent, however the next read of isRestricted should be correct.
            }
        }
    }
}

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

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.

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:

reachableViaWWAN
reachableViaWiFi 

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

– setReachabilityStatusChangeBlock:

AFNetworking Github

AFNetworkReachabilityManager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!