2016-08-01 15 views
3

私のアプリにインターネット接続の切断を自動的に検出させたい。だから私は次のコードを使用しています。インターネット接続を継続的に検出する

- (void)applicationDidBecomeActive:(UIApplication *)application { 

    Reachability *networkReachability = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus]; 
    if (networkStatus == NotReachable) { 
     [Settings hideSpinner]; 
     //Show no internet connectivity dialog. 
    } else { 
    } 
} 

しかし、問題はインターネット接続を絶えずチェックしていないことです。 アプリがアクティブになったときのみチェックします。アプリのライフサイクル全体を通じてインターネット接続を継続的に確認し、インターネットが自動的に切断されると警告を表示するにはどうすればよいですか?

+0

到達可能性は、状態が変化したときに実行されるコールバックを提供します。それを使用してください。 – Avi

答えて

2

このようにobacherverをReachabilityメソッドに追加します。

1)[NSNotificationCenter defaultCenter] addObserver:自己セレクタ:@selector(reachabilityChanged :) name:kReachabilityChangedNotificationオブジェクト:なし];

アプリがバックグラウンドモードで開くと自動的に呼び出され、reachabilityChangedが呼び出されます。

2)[[NSNotificationCenter defaultCenter] postNotificationName:@ "ChangeInternetConnection"オブジェクト:なし];

ChangeInternetConnectionあなたはインターネットがそれは状況の変化するときの状態を得るためにあなたののViewControllerにオブザーバを追加する必要があります。

- (void) checkInternetConnetion { 

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; 

     //NSString *remoteHostName = @"www.apple.com"; 


    self.internetReachability = [Reachability reachabilityForInternetConnection]; 
     [self.internetReachability startNotifier]; 
     [self updateInterfaceWithReachability:self.internetReachability]; 
    } 


    #pragma mark - Reachability Methods 

    - (void)updateInterfaceWithReachability:(Reachability *)reachability { 
    if (reachability == self.internetReachability) { 
      [self checkStatus:reachability]; 
     } 

     if (reachability == self.wifiReachability) { 
      [self checkStatus:reachability]; 
     } 
    } 


    -(void)checkStatus :(Reachability *)reachability { 

     NetworkStatus netStatus = [reachability currentReachabilityStatus]; 
     BOOL connectionRequired = [reachability connectionRequired]; 
     NSString* statusString = @""; 

     switch (netStatus) { 

      case NotReachable: { 

       self.isInternetOn = FALSE; 

       break; 
      } 

      case ReachableViaWWAN: { 
       self.isInternetOn = TRUE; 

       break; 
      } 
      case ReachableViaWiFi: { 
       self.isInternetOn = TRUE; 

       break; 
      } 
     } 



     [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeInternetConnection" object:nil]; 

     } 

    - (void) reachabilityChanged:(NSNotification *)note { 

     Reachability* curReach = [note object]; 
     NSParameterAssert([curReach isKindOfClass:[Reachability class]]); 
     [self updateInterfaceWithReachability:curReach]; 
    } 
+0

これは、インターネットが利用可能かどうかをチェックするためのサイトを読み込むベストプラクティスではないため、アプリのパフォーマンスが低下するためです – Learner

+1

ありがとう... Btw私はどこでもその変数を使用していません。 – Bhoomi

+0

okありがとう...... – Learner

1

まず、あなたのクラスのインポート:#import "Reachability.h"

その後は、道を次のように実行します。

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(reachabilityChanged:) 
               name:kReachabilityChangedNotification object:nil]; 


-(BOOL)reachabilityChanged:(NSNotification*)note 
{ 
    BOOL status =YES; 

    NSLog(@"reachabilityChanged"); 

    Reachability * reach = [note object]; 

    if([reach isReachable]) 
    { 
     status = YES; 

     NSLog(@"your network is Available"); 
    } 

    else 

    { 
     status = NO; 

//Do something here 

    } 
    return status; 
} 
+0

これをバックグラウンドで継続的に検出するにはどうすればよいですか? – Learner

1

タイマーがこれを行うための効率的な方法ではありませんが、到達可能性の変更通知のためのオブザーバーを追加しますタイマーも使えます。

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
     NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f 
                target:self 
               selector:@selector(handleConnectivity) 
               userInfo:nil 
               repeats:YES]; 
} 
-(void)handleConnectivity 
{ 
    Reachability *networkReachability = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus]; 
    if (networkStatus == NotReachable) 
    { 
     //No internet connectivity - perform required action 
    } 
    else 
    { 
     //Internet connectivity is valid 
    } 
} 

最も良い方法は、到達可能性コードを使用することです。インターネットの利用、無線LAN/WANの接続性チェックなどを確認するために便利なメソッドをたくさん持っているCheck here for apple sample code. ..例えば

: -

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChanged:) name:kReachabilityChangedNotification object:nil]; 

reachability = [Reachability reachabilityForInternetConnection]; 
[reachability startNotifier]; 

- (void)networkChanged:(NSNotification *)notification 
{ 

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; 

    if(remoteHostStatus == NotReachable) { NSLog(@"not reachable");} 
    else if (remoteHostStatus == ReachableViaWiFiNetwork) { NSLog(@"wifi"); } 
    else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { NSLog(@"carrier"); } 
} 

あなたがバックグラウンドでのみ、この事を確認することができます

* audio - アプリケーションは、バックグラウンドでユーザーに可聴コンテンツを再生します。 (このコンテンツには、AirPlayを使用してオーディオやビデオコンテンツをストリーミングする が含まれます)

* location - アプリはバックグラウンドで実行中であってもユーザーにその位置を通知します。

* voip - このアプリでは、インターネット接続を使用して電話をかけることができます。

* newsstand-content:このアプリは雑誌や新聞のコンテンツをバックグラウンドでダウンロードして処理するニューススタンドアプリです。

*外部アクセサリ - このアプリケーションは、外部 アクセサリフレームワークを通じて定期的にアップデートを提供する必要があるハードウェアアクセサリで動作します。

* bluetooth-central - このアプリケーションは、Core Bluetooth フレームワークを通じて定期的にアップデートを提供する必要があるBluetoothアクセサリで動作します。

* bluetooth-peripheral:このアプリケーションは、Core Bluetoothフレームワークを通じて周辺モードでBluetooth通信をサポートしています。

+0

これをバックグラウンドで継続的に実行できますか? – Learner

+0

これはバックグラウンドでのみ行うことができます – PinkeshGjr

3

アプリケーションが起動したら、あなたは同じことを行うためにNSTimerを発射することができます

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f 
               target:self 
              selector:@selector(checkForConnectivity) 
              userInfo:nil 
              repeats:YES]; 
} 

-(void)checkForConnectivity { 
     Reachability *networkReachability = [Reachability reachabilityForInternetConnection]; 
     NetworkStatus networkStatus = [networkReachability currentReachabilityStatus]; 
     if (networkStatus == NotReachable) { 
       //No internet connectivity - perform required action 
     } 
     else { 
       //Internet connectivity is valid 
     } 
} 

ありがとう!

1

オブザーバーを追加します。

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(reachabilityChanged:) 
              name:kReachabilityChangedNotification 
              object:nil]; 

-(BOOL)reachabilityChanged:(NSNotification*)note 
{ 
    BOOL status =YES; 
    NSLog(@"reachability is changed"); 

    Reachability * reach = [note object]; 

    if([reach isReachable]) 
    { 
     status = YES; 
     NSLog(@"NetWork is Available. Please go ahead"); 
    } 
    else 
    { 
     status = NO; 
     NSLog(@"NetWork is not Available. Please check your connection."); 
    } 
    return status; 
} 
-1

iOSからReachabilityフレームワークを利用して、NSTimerと組み合わせることができます。

関連する問題