0

私が達成しようとしているのは、インターネット接続を監視することですか? そのため、電話にはインターネットへのアクティブな接続が必要です。再試行のオプションを含む UIAlertView が表示される場合 (接続を再試行して、変更されたかどうかを確認します)。

到達可能性と api.parse.com リンクへの接続を使用しようとしています。

私の AppDelegate では、次のように Reachability のセットアップを呼び出します。

// Use Reachability to monitor connectivity
[self monitorReachability];

monitorReachability は次のように設定されます。

- (void)monitorReachability {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:ReachabilityChangedNotification object:nil];

self.hostReach = [Reachability reachabilityWithHostName: @"api.parse.com"];
[self.hostReach startNotifier];

self.internetReach = [Reachability reachabilityForInternetConnection];
[self.internetReach startNotifier];

self.wifiReach = [Reachability reachabilityForLocalWiFi];
[self.wifiReach startNotifier];

}

また、次のように到達可能性を変更した方法もあります: EDIT - 更新された方法

- (void)reachabilityChanged:(NSNotification* )note {
Reachability *curReach = (Reachability *)[note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NSLog(@"Reachability changed: %@", curReach);
networkStatus = [curReach currentReachabilityStatus];

if (networkStatus == NotReachable) {
    NSLog(@"NOT REACHABLE");
    return;
} else {
    NSLog(@"REACHABLE");
}

私が理解しようとしているのは、返された応答です。上記から、現在のステータスへのポインターがあるように見えますが、これを使用する方法がわかりません。基本的に、そのリンクがインターネット接続を介して到達可能かどうかをifステートメントで確認したいのですが、そうでない場合はAlertViewを介して到達できます。次に、UIAlertView が使用するブール値、つまり、showingConnectionAlert を設定できます。これは、接続が変更されて取得されたときにダウンさせることができます。これもどこに置くか迷っています。

4

1 に答える 1

0

Reachability クラスを使用する最も簡単な方法の 1 つは、Reachability.h を rootViewController または接続が必要になるものにインポートしてから、このコードを実行することです...

 Reachability *reach = [Reachability reachabilityForInternetConnection];

 NetworkStatus netStatus = [reach currentReachabilityStatus];    
 if (netStatus == NotReachable) {        
 NSLog(@"No internet connection!");   
//Alert View in here      
} 
else {        
//Do something in here with the connecion e.g:

[self performSelector:@selector(startNSURLRequest) withObject:nil afterDelay:30.0];

}        

これにより、プロセスが少し簡素化されるはずです。T

于 2013-03-11T21:34:06.433 に答える