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

后端 未结 6 1072
别那么骄傲
别那么骄傲 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:57

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

提交回复
热议问题