Live you cannot change if the network connection changed, the very best you can do to make it work is to use Background Fetch
mode from Capabilities
. Firstable you need to check the checkbox for background mode:
Then you need to ask for time interval as often as you can the sooner the better so i suggest application:didFinishLaunchingWithOptions:
and you need to put this line:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
The UIApplicationBackgroundFetchIntervalMinimum
it's as often as possible, but it's not exact number of seconds between fetches from the docs:
The smallest fetch interval supported by the system.
And then when background fetch will fire you can check in AppDelegate
with method:
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
switch (status) {
case NotReachable: {
NSLog(@"no internet connection");
break;
}
case ReachableViaWiFi: {
NSLog(@"wifi");
break;
}
case ReachableViaWWAN: {
NSLog(@"cellurar");
break;
}
}
completionHandler(YES);
}
All of this will work in iOS 7.0 or higher.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…