2010-10-17 60 views
3

我要檢查一個有效的網絡連接。我跟着蘋果的可達性例子,把我的支票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的問題在這裏:Application Failed to Launch in Time

我不確定我應該在哪裏執行可達性檢查?

回答

0

-applicationDidBecomeActive你可以調用一個方法在使用可達代碼-performSelectorInBackground:withObject:背景

+0

爲什麼我不能把它放在ApplicationDidFinishLaunching中? – 2010-10-17 17:48:44

+1

每次應用程序激活時你都不想測試它嗎?使用iOS 4多任務處理可能會發生很多事情。 – asandroq 2010-10-18 01:54:51

3

阿可達性檢查可以根據網絡條件需要很長時間顯著量(30秒以上)。但是,如果你的應用程序的用戶界面不會爲一些秒數(超過30少得多)響應,OS假定它是死了,殺死它。

如果你做你的可達性檢查在後臺線程,而不是UI線程,那麼你的UI會保持響應,並且無論是操作系統還是用戶就會認爲您的應用程序已鎖定或崩潰。

+0

我在後臺線程中執行它,仍然得到了同樣的錯誤:http://stackoverflow.com/questions/3979065/application-failed-to-launch-in-time-even-with-a-background-thread – 2010-10-20 14:43:18