Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 364
LodgeApps Avatar asked Sep 27 '22 12:09

LodgeApps


1 Answers

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?

like image 200
Redssie Avatar answered Nov 05 '22 02:11

Redssie