[英]Checking the Internet Connection iOS App
在初始屏幕中,我訂閱了UIApplicationDidBecomeActiveNotification。
- (void)viewDidLoad
{
[super viewDidLoad];
// register notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkInternetConnection:) name:UIApplicationDidBecomeActiveNotification object:nil];
}
-(void) checkInternetConnection:(NSNotification *) notification
{
internetStatus = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];
if(internetStatus == NotReachable)
{
[AppHelper showAlert:@"ERROR" message:@"Error downloading data. Please check your network connection and try again." cancelButtonTitle:nil otherButtonTitles:@[@"OK"]];
return;
}
[self viewDidAppear:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if(!(internetStatus == NotReachable))
{
AppDelegate *applicationDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[applicationDelegate performSelector:@selector(showMainMenu) withObject:[NSNumber numberWithBool:YES] afterDelay:3.0];
}
}
問題是只能在初始屏幕上檢查Internet連接,因此,如果我在其他屏幕中間,並且失去了Internet連接,則無法判斷該連接已消失。 我如何制定一個良好的邏輯來檢查客戶端的Internet連接。
您想使用一個可訪問性庫,該庫可讓您獲取有關更改的回調( 如本示例 )。
然后,在需要它的每個控制器中,創建一個可達性實例,並設置reachableBlock
和unreachableBlock
來執行所需的操作。
我建議創建一個所有互聯網請求都經過過濾的類,並檢查該類中的連接性(使用建議的可到達性)。 如果以這種方式進行架構,則應該可以簡化一些事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.