How to check if the connection was switched to 3G when the application is in the background in iOS?

Is it possible for the application to find out if the connection has been switched from Wi-Fi to 3G. When is the application in the background? The requirement is that the download must happen when connecting to Wi-Fi. Verification is done first, but I need to make sure that the download is over Wi-Fi and should be canceled if the user switched to 3G / 4G when the application is in the background.

+4
source share
2 answers

If you just want the downloads to download only in WiFi, you should set the allowsCellularAccessNSURLSession NSURLSessionConfiguration object property to NO.

No monitoring, cancellation, or other tricks are required: make sure the download never goes through cellular.

+7
source

I offer you this library

It uses NSNotification, and you can make any changes to it if you need to install it in your own way.

This is an example of the code provided by this library.

// Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
reach.reachableOnWWAN = NO;

// Here we set up a NSNotification observer. The Reachability that caused the notification
// is passed in the object parameter
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(reachabilityChanged:) 
                                             name:kReachabilityChangedNotification 
                                           object:nil];

[reach startNotifier];

Hope this helps

0
source

Source: https://habr.com/ru/post/1537669/


All Articles