3

我想检查有效的网络连接。我按照 Apple 的 Reachability 示例并签入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;
    }

但是,我的应用程序有时会因错误而崩溃Application Failed to Launch in Time

我在这里发布了我的崩溃作为一个 SO 问题:应用程序无法及时启动

我不确定我应该在哪里执行可达性检查?

4

2 回答 2

3

根据网络状况,可达性检查可能需要大量时间(30 秒或更长时间)。但是,如果您的应用程序的 UI 在几秒钟内(远少于 30 秒)没有响应,则操作系统会假定它已死并杀死它。

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.

于 2010-10-17T18:17:37.757 回答
0

您可以在后台-applicationDidBecomeActive调用使用可达性代码的方法。-performSelectorInBackground:withObject:

于 2010-10-17T15:03:54.623 回答