简体   繁体   中英

Where should I perform a Reachability check?

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: Application Failed to Launch in Time

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

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.

-applicationDidBecomeActive您可以在后台调用一个方法,该方法将可访问性代码与-performSelectorInBackground:withObject:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM