简体   繁体   中英

Checking if Cellular Data has been turned off

So I've done a lot of research and I haven't quite found the answer I have been looking for...

I want to make it so that, if cellular data is turned off for my app in particular , the app doesn't work or at least make it so a few buttons are hidden.

I have reachability put in place so that if there is no internet connection, the app does not run. However, if the user is connected to the internet but has cellular data turned off for my app , and they are not connected to wifi, then the app runs (which I do not want it to do).

Any help would be highly appreciated!

I had a similar problem and found this solution. Hopefully it helps.

func isCellularRestricted() {
        let cellState = CTCellularData.init()
        cellState.cellularDataRestrictionDidUpdateNotifier = { (dataRestrictedState) in
            if dataRestrictedState == CTCellularDataRestrictedState.restricted { // State has changed
                // your app doesn't have cellular data
            } else if dataRestrictedState ==  CTCellularDataRestrictedState.notRestricted {
                // your app does have cellular data
            }
        }
    }

This is the function I implemented in my app delegate (called from didFinishLaunchingWithOptions). It gets called everytime user changes cellular data for your app and only for your app. If user allows cellular data for your app and has turned off cellular data in iOS, this will still return notRestricted.

In my case I made a constant which I set to true/false, depending if cellular data is allowed.

I found the answer somewhere on SO but I cannot find the link.

EDIT (found link): How do I know if cellular access for my iOS app is disabled?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM