我应该在哪里进行可达性检查?
我想检查有效的网络连接。 我按照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
崩溃
我在这里发布了我的崩溃问题: 应用程序无法及时启动
我不确定应该在哪里执行可达性检查?
在-applicationDidBecomeActive
您可以在后台调用一个方法,该方法使用带有-performSelectorInBackground:withObject:
的可访问性代码-performSelectorInBackground:withObject:
.
可达性检查可能需要很长时间(30秒或更长时间),具体取决于网络状况。 但是,如果您的应用程序的UI没有响应几秒钟(远小于30秒),则操作系统会假定它已经死亡并将其杀死。
如果您在后台线程而不是UI线程中进行可达性检查,那么您的UI将保持响应,操作系统和用户都不会认为您的应用已被锁定或崩溃。