views:

82

answers:

2

I want to check for a valid network connection. I followed Apple's Reachability example and put my check in applicationDidFinishLaunching

#pragma mark -
#pragma mark Application lifecycle

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

        if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled")) 
        {
            NSLog(@"NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled enabled!");
        }

        // Override point for customization after application launch.
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

        //Check for connectivity
        internetReach = [[Reachability reachabilityForInternetConnection] retain];
        [internetReach startNotifer];
        [self updateInterfaceWithReachability: internetReach];

        [window addSubview:navigationController.view];
        [window makeKeyAndVisible];

        return YES;
    }

However, my app will crash sometimes with the error Application Failed to Launch in Time

I have posted my crash as an SO question here: http://stackoverflow.com/questions/3908882/application-failed-to-launch-in-time

I'm not sure where I should perform the reachability check?

A: 

In -applicationDidBecomeActive you may call a method in the background that uses the reachability code with -performSelectorInBackground:withObject:.

asandroq
why can't i put it in ApplicationDidFinishLaunching?
Sheehan Alam
Don't you want to test it everytime the application becomes active? With iOS 4 multitasking, that can happen a lot.
asandroq
+2  A: 

A Reachability check could take a significant amount of time (30 seconds or more) depending on network conditions. But if your app's UI does not respond for some number of seconds (much less than 30), the OS assumes that it is dead and kills it.

If you do your Reachability check in a background thread, not the UI thread, then your UI will stay responsive, and neither the OS nor the user will assume that your app has locked up or crashed.

hotpaw2
i perform it in a background thread and still get the same error: http://stackoverflow.com/questions/3979065/application-failed-to-launch-in-time-even-with-a-background-thread
Sheehan Alam