5

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!

Vignan Sankati
  • 380
  • 5
  • 15
LodgeApps
  • 344
  • 1
  • 2
  • 21
  • 2
    So you want your app to use internet access only if it comes through cellular data rather than through Wifi/Bluetooth? Why? Telling us why might help with a solution or strategy. – Basil Bourque Jun 27 '15 at 02:21
  • 1
    No, that is not the case. If there is no internet whatsoever, I want to hide a button. So, if a user is connected to wifi, then great, they can use the app. If they are connected to cellular data, then great, they can use the app. However, if they turn off their wifi, and turn off cellular data for my app specifically, they should not be able to use the app because ads will not show up. The reachability I have put in place essentially says, the device is connected to internet, so you can do internet stuff. When the cellular data is turned off for my app only, reachability reports this falsely – LodgeApps Jun 28 '15 at 18:46
  • Sorry if my question was misleading or confusing before, I hope that clears it up @BasilBourque – LodgeApps Jun 28 '15 at 18:46
  • Correct me if I am just wayyy over simplifying this... But can you not just ping some server to see if the device is connected to the internet? – Trevor Clarke May 09 '18 at 16:45
  • As suggested by @TrevorClarke . See https://stackoverflow.com/questions/46482545/swift-how-to-check-if-my-app-has-wlan-permission-and-the-device-is-connected-to – Warren Burton May 10 '18 at 21:51

1 Answers1

4

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?

Redssie
  • 181
  • 1
  • 12