Apple のエンジニアは、Rechability に完全に依存することを提案しています。
SO投稿から(ブロック引用のソース)
On a WWDC talk this year the Apple engineer on stage recommended users to never base
the application internet access on the Reachability example app status. Often
reachability doesn't provide a complete information (it is based on a complex mechanism)
and the suggestion provided by the engineer was this:
1. try to do your internet connection, whatever it is the Reachability status;
then set your UI hint based on success/fail result
2. if it fails due to networking issue, then register to Reachability and retry again
when Reachability gives the green light; this is needed when you want to recover
automatically from the fail condition
3. in any case give the user the possibility to "force a retry", whatever is the
Reachability status. If it succeeds, reset your UI hint immediately.
私がやった事 ?
たとえば、接続を確立する必要があるたびに
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString
stringWithFormat:@"http://myadress.com"]]];
[self performSelectorOnMainThread:@selector(responseHandler:)
withObject:data waitUntilDone:TRUE];
- (void)responseHandler:(NSData *)responseData {
if(!responseData) {
ReachabilityController *reachability = [[ReachabilityController alloc] init];
[reachability checkReachability];
return;
}
// you handle your data
}
そこで起こっていることは、接続が失敗した場合にのみ到達可能性がテストされるということです。到達可能性のみを扱う汎用の ReachabilityController を作成しました。リクエストを行うたびに、他のすべてのコントローラーから呼び出しを行うことができるように、そうしました。
私の ReachabilityController.m は次のようになります
-(void) checkReachability {
Reachability* internetAvailable = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [internetAvailable currentReachabilityStatus];
NSString *messageText;
if (netStatus == NotReachable)
{
messageText = [NSString stringWithFormat:@"Internet access not available"];
}
else
{
Reachability *netReach = [Reachability reachabilityWithHostName:host];
NetworkStatus hostStatus = [netReach currentReachabilityStatus];
if (hostStatus == NotReachable)
{
messageText = [NSString stringWithFormat:@"Host Unreachable"];
}
else
{
messageText = [NSString stringWithFormat:@"Problem with remote service"];
}
}
NSLog(@"%@", messageText);
}
「nil」データパラメータを自分で処理していて、最終的に原因を特定しているため、アプリがクラッシュすることはありません。
お役に立てれば!
アップデート:
インターネット接続に関する誤ったメッセージを表示した場合、Apple はアプリを拒否することがあります。彼らはiPhoneでの評判について非常に真剣です. インターネット接続が利用可能であるが、インターネット接続が利用できないとアプリが報告する場合、それは非常に深刻であると見なされます