Question

Is it Possible for an app to know whether the connection has been switched from Wi-Fi to 3G While app is in background?The requirement is that download should happen on Wi-Fi Connection.The Check is done initialy but i need to confirm that download is happening on Wi-Fi and should be cancelled if the user has switched to 3G/4G while app is in Background.

Was it helpful?

Solution

If you just want to make sure downloads only happen on WiFi, then you should set the allowsCellularAccess property of the NSURLSession's NSURLSessionConfiguration to NO.

No monitoring, cancelling or other tricks needed: that'll make sure the download never goes over the cellular connection.

OTHER TIPS

I suggest you this library

It uses NSNotification, and you can make some changes in it if you need to set it your way

This is a example 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 it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top