ios之網路監聽

來源:互聯網
上載者:User
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  4.       
  5.     //開啟網路狀況的監聽   
  6.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];  
  7.       
  8.     self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;  
  9.     [self.hostReach startNotifier];  //開始監聽,會啟動一個run loop   
  10.   
  11.     self.window.rootViewController = self.tabBarController;  
  12.     [self.window makeKeyAndVisible];  
  13.     return YES;  
  14. }  
  15.   
  16. //網路連結改變時會調用的方法   
  17. -(void)reachabilityChanged:(NSNotification *)note  
  18. {  
  19.     Reachability *currReach = [note object];  
  20.     NSParameterAssert([currReach isKindOfClass:[Reachability class]]);  
  21.       
  22.     //對串連改變做出響應處理動作   
  23.     NetworkStatus status = [currReach currentReachabilityStatus];  
  24.     //如果沒有串連到網路就彈出提醒實況   
  25.     self.isReachable = YES;  
  26.     if(status == NotReachable)  
  27.     {  
  28.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"網路連接異常" message:@"暫無法訪問書城資訊" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];  
  29.         [alert show];  
  30.         [alert release];  
  31.         self.isReachable = NO;  
  32.     }  
  33.     else  
  34.     {  
  35.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"網路連接資訊" message:@"網路連接正常" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];  
  36.         [alert show];  
  37.         [alert release];  
  38.         self.isReachable = YES;  
  39.     }  
  40. }  
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];        //開啟網路狀況的監聽    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];        self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;    [self.hostReach startNotifier];  //開始監聽,會啟動一個run loop    self.window.rootViewController = self.tabBarController;    [self.window makeKeyAndVisible];    return YES;}//網路連結改變時會調用的方法-(void)reachabilityChanged:(NSNotification *)note{    Reachability *currReach = [note object];    NSParameterAssert([currReach isKindOfClass:[Reachability class]]);        //對串連改變做出響應處理動作    NetworkStatus status = [currReach currentReachabilityStatus];    //如果沒有串連到網路就彈出提醒實況    self.isReachable = YES;    if(status == NotReachable)    {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"網路連接異常" message:@"暫無法訪問書城資訊" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];        [alert show];        [alert release];        self.isReachable = NO;    }    else    {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"網路連接資訊" message:@"網路連接正常" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];        [alert show];        [alert release];        self.isReachable = YES;    }}

通過如上代碼,在應用程式的任何一個介面都可以使用下面的單例來判斷網路是否串連

 

 

[cpp] view plaincopyprint?
  1. AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];  
  2. if(appDlg.isReachable)  
  3. {  
  4.     NSLog(@"網路已串連");//執行網路正常時的代碼   
  5. }  
  6. else  
  7. {  
  8.     NSLog(@"網路連接異常");//執行網路異常時的代碼   
  9. }  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.