一般的に、デバイスがインターネットに接続できるかどうかを確認しようとしている場合は、reachabilityWithHostName: ではなく、reachabilityForInternetConnection を使用する必要があります。また、これらの呼び出しはどちらも起動に少し時間がかかります (それでもミリ秒単位ですが、次の行の if 条件に到達するのにかかる時間よりも長くなります)。到達可能性を使用します。
static NetworkManager* sharedInstance = nil;
@interface NetworkManager()
@property (nonatomic, strong) Reachability* reachability;
@end
@implementation NetworkManager
@synthesize reachability;
+ (NetworkManager*)sharedInstance
{
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [[NetworkManager alloc] init];
}
}
return sharedInstance;
}
- (id)init
{
reachability = [WYReachability reachabilityForInternetConnection];
}
- (BOOL)isNetworkReachable
{
return ([self.reachability currentReachabilityStatus] != NotReachable);
}
@end
使用できる他のクラスで到達可能なネットワークを確認するには。
[[NetworkManager sharedInstance] isNetworkReachable];