2017-10-16 43 views
0

我是新來的目標c。我發現了很多方法來檢查互聯網連接,如可達性測試,使用AF網絡等。這是最好的方法嗎?任何幫助?哪一個是最好的方法來檢查目標中的網絡可達性c

+0

可以使用可達性。 –

+0

下載可達性類 https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html –

回答

0

我總是比較喜歡使用可達性https://github.com/tonymillion/Reachability。 GitHub上的描述對於你爲什麼要使用它是如此清楚 - >「這是蘋果Reachability類的直接替代品,它與ARC兼容,它使用新的GCD方法來通知網絡接口更改。」

Reachability *internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"]; 

// Internet is reachable 
internetReachableFoo.reachableBlock = ^(Reachability*reach) 
{ 
    // Update the UI on the main thread 
    dispatch_async(dispatch_get_main_queue(), ^{ 

     //do something 
     NSLog(@"REACHABLE!"); 

     } error:^(NSError * _Nullable error) { 
      NSLog(@"Error:%@ ", [error localizedDescription]); 
     }]; 
    }); 
}; 

// Internet is not reachable 
internetReachableFoo.unreachableBlock = ^(Reachability*reach) 
{ 
    // Update the UI on the main thread 
    dispatch_async(dispatch_get_main_queue(), ^{ 

     //do something 
     NSLog(@"UNREACHABLE!"); 

     }); 
}; 

[internetReachableFoo startNotifier]; 

好運:)

相關問題