Force Mobile Data usage on some API calls and other to be through wifi or the available network

1k views Asked by At

I am working on an application where there is a need to do a specific API only through Mobile Data and not wifi, allowing other API calls to be done using the available network type whether cellular data or wifi or ....

Is there a way to force use Mobile data usage even if WIFI is ON on this API call.

After opening a ticket with apple developer this was their reply:

Yes. When using Network Framework, take a look at requiredInterfaceType on NWConnection. For higher level APIs like URLSession, this would be an Enhancement Request.

Yet i am still stuck and do not know how to proceed.

1

There are 1 answers

1
Jon Shier On

You can get a bit of control here by setting the allowsConstrainedNetworkAccess or allowsExpensiveNetworkAccess on your URLRequests on iOS 13 (and same year Apple OSes) and above.

allowsConstrainedNetworkAccess:

In iOS 13 and later, users can set their device to use Low Data Mode as one of the Cellular Data Options in the Settings app. Users can turn on Low Data Mode to reduce your app’s network data usage. This property controls the request’s behavior when the user turns on Low Data Mode. If there are no nonconstrained network interfaces available and the request’s allowsConstrainedNetworkAccess property is false, any connection created from the request fails. In this case, the error provided when the connection fails has a networkUnavailableReason property whose value is NSURLErrorNetworkUnavailableReason.constrained.

Setting this property on a request overrides the allowsConstrainedNetworkAccess property of URLSessionConfiguration. For example, if the session configuration’s allowsConstrainedNetworkAccess value is false, and you create a task from a request whose allowsConstrainedNetworkAccess is true, the task treats the value as true.

Limit your app’s of use of constrained network access to user-initiated tasks, and put off discretionary tasks until a nonconstrained interface becomes available.

allowsExpensiveNetworkAccess:

The system determines what constitutes “expensive” based on the nature of the network interface and other factors. iOS 13 considers most cellular networks and personal hotspots expensive. If there are no nonexpensive network interfaces available and the request’s allowsExpensiveNetworkAccess property is false, any task created from the request fails. In this case, the error provided when the task fails has a networkUnavailableReason property whose value is NSURLErrorNetworkUnavailableReason.expensive.

Setting this property on a request overrides the allowsExpensiveNetworkAccess property of URLSessionConfiguration. For example, if the session configuration’s allowsExpensiveNetworkAccess value is false, and you create a task from a request whose allowsExpensiveNetworkAccess is true, the task treats the value as true.

Limit your app’s of use of expensive network access to user-initiated tasks, and put off discretionary tasks until a nonexpensive interface becomes available.