1

I have created an iPhone application Where i want to check internet connectivity.At the didFinishLaunchingWithOptions method of the app delegate method i wrote

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    viewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" title:firstTabTitleGlobal bundle:nil];
    viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" title:secondTabTitleGlobal bundle:nil];

    newNavController = [[UINavigationController alloc] initWithRootViewController:viewController1];

    userNavController = [[UINavigationController alloc] initWithRootViewController:viewController2];

    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:newNavController,userNavController,nil]

    Reachability *r = [Reachability reachabilityWithHostName:globalHostName];

    NetworkStatus internetStatus = [r currentReachabilityStatus];

    if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
    {
        [self showAlert:globalNetAlertTitle msg:globalNetAlertMsg];
        [activityIndicator stopAnimating];
    }
    else
    {
        [activityIndicator stopAnimating];
        self.window.rootViewController = self.tabBarController;
        [self.window makeKeyAndVisible];
    }
}

My Code is ok because when no internet connectivity then show alert.But problem is when no interner then default.png is shown. When i run apps again then the apps runs from the showing default.png. And nothing happen. Thanks in advance.

mopsled
  • 8,445
  • 1
  • 38
  • 40
dulal_026
  • 589
  • 1
  • 6
  • 20

3 Answers3

2

application:didFinishLaunchingWithOptions: will only run when an app is launched.

If you want your application to check for availability on subsequent application activation, try putting your code in applicationDidBecomeActive:

mopsled
  • 8,445
  • 1
  • 38
  • 40
  • 2
    I think your answer fixes the problem set out by the asker - the reason for the up vote, but using NSNotifications is much more practical and useful in providing dynamic updates, rather than having to manually check the internet connection. – Jonathan King Jun 24 '12 at 16:11
1

What might be better is using NSNotifications, to dynamically tell you if you have connectivity. You can do this with an apple class called 'reachabilty'. Once you have included the file in your project, you can then use something like this;

//in viewDidOnload    
[[NSNotificationCenter defaultCenter] addObserver:self 
                                          selector:@selector(handleNetworkChange:) 
                                          name:kReachabilityChangedNotification object:nil];
reachability = [[Reachability reachabilityForInternetConnection] retain];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];

if (status == NotReachable) {
    //Do something offline
} else {
    //Do sometihng on line
}

- (void)handleNetworkChange:(NSNotification *)notice{
 NetworkStatus status = [reachability currentReachabilityStatus];
 if (status == NotReachable) {
  //Show offline image
 } else {
  //Hide offline image
 }

}

(this is the corrected code from Reachability network change event not firing)

You will then be able to update you images as soon as any network change takes place. However don't forget to remove yourself from receiving notifications in the dealloc with;

[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];

If you need any more information about how to implement this, I would be happy to help!

Jonathan

Community
  • 1
  • 1
Jonathan King
  • 1,528
  • 14
  • 25
0

This works great in Swift 5. Returns a Boolean. It also works on mobile data.

public class Reachability {

        class func isConnectedToNetwork() -> Bool {

            var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
            zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
            zeroAddress.sin_family = sa_family_t(AF_INET)

            let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
                $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
                    SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
                }
            }

            var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
            if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
                return false
            }

            /* Only Working for WIFI
             let isReachable = flags == .reachable
             let needsConnection = flags == .connectionRequired

             return isReachable && !needsConnection
             */

            // Working for Cellular and WIFI
            let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
            let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
            let ret = (isReachable && !needsConnection)

            return ret

        }
    }

hope this helps.

Lexter
  • 33
  • 8