2013-04-07 106 views
0

這種情況非常簡單,我的應用程序會在它變爲活動狀態時查找信息。大多數情況下,它只是簡單地使用緩存,但是在某些情況下,它需要Internet連接來請求所需的信息。應用程序需要等待Internet連接

我已經使用Reachability(https://github.com/tonymillion/Reachability)來確定是否有可用的活動連接。問題在於,iPhone需要幾秒鐘時間才能激活連接。這意味着該應用程序看到沒有可用的連接並會顯示錯誤消息。

我想什麼發生的是,應用程序將首先檢查是否有可用的連接:

Reachability *reachability = [Reachability reachabilityWithHostname:URL_LOTTOTALL_WEB]; 
NetworkStatus internetStatus = [reachability currentReachabilityStatus]; 
if (internetStatus == NotReachable) { 
} else { 
} 

如果沒有連接可用,我想在幾秒鐘內(也許2或重試3)。如果仍然沒有連接可用,則顯示錯誤消息。任何建議來實現這個簡單的實現?

+0

您也可以嘗試使用while循環 – Jona 2013-04-07 16:50:47

回答

3

嘗試使用Reachability的回調塊,如 this answer中所述。

internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"]; 
// Internet is reachable 
internetReachableFoo.reachableBlock = ^(Reachability*reach) 
{ 
    // Update the UI on the main thread 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     NSLog(@"Yayyy, we have the interwebs!"); 
    }); 
}; 

// Internet is not reachable 
internetReachableFoo.unreachableBlock = ^(Reachability*reach) 
{ 
    // Update the UI on the main thread 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     NSLog(@"Someone broke the internet :("); 
    }); 
}; 

[internetReachableFoo startNotifier]; 
+0

謝謝,我得到它與類似的解決方案。 – 2013-04-07 16:34:39