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

后端 未结 6 1048
别那么骄傲
别那么骄傲 2020-11-29 04:21

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

6条回答
  •  暖寄归人
    2020-11-29 04:52

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

提交回复
热议问题