
我想出了一个办法做到这一点使用NSURL。我的方式似乎有点不可靠(因为即使谷歌可以有一天下来,依靠第三方似乎不好),虽然我可以检查看到一些其他网站的响应,如果谷歌没有回应,看起来浪费和我的申请不必要的开销。
- (BOol) connectedToInternet{ Nsstring *URLString = [Nsstring stringWithContentsOfURL:[NSURL URLWithString:@"http://www.Google.com"]]; return ( URLString != NulL ) ? YES : NO;} 是我做的坏吗? (更不用说stringWithContentsOfURL在iOS 3.0和OSX 10.4中已弃用),如果是这样,什么是更好的方法来完成这个?
解决方法 方法1:使用一个简单的(ARC和GCD兼容)类来做1)向项目添加SystemConfiguration框架,但不要担心将其包含在任何地方
2)将Tony Million的Reachability.h和Reachability.m版本添加到项目中(在这里:https://github.com/tonymillion/Reachability)
3)更新接口部分
#import "Reachability.h"// Add this to the interface in the .m file of your vIEw controller@interface MyVIEwController (){ Reachability *internetReachableFoo;}@end 4)然后在您可以调用的视图控制器的.m文件中实现此方法
// Checks if we have an internet connection or not- (voID)testInternetConnection{ 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(),^{ NSLog(@"Yayyy,we have the interwebs!"); }); }; // Internet is not reachable internetReachableFoo.unreachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(),^{ NSLog(@"Someone broke the internet :("); }); }; [internetReachableFoo startNotifIEr];} 方法2:使用Apple的过时的Reachability类自己做一下
1)向项目添加SystemConfiguration框架,但不要担心将其包含在任何地方
2)将Apple的Reachability.h和Reachability.m版本添加到项目(you can get those here)
3)添加@class可达性;到你正在实现代码的.h文件
4)创建几个实例以检入.h文件的接口部分:
Reachability* internetReachable;Reachability* hostReachable;
5)在.h中添加一个方法,用于网络状态更新时:
-(voID) checkNetworkStatus:(NSNotification *)notice;
6)将#import“Reachability.h”添加到要实现检查的.m文件
7)在要实现检查的.m文件中,可以将其放在第一个调用的方法(init或vIEwWillAppear或vIEwDIDLoad等)中:
-(voID) vIEwWillAppear:(BOol)animated{ // check for internet connection [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangednotification object:nil]; internetReachable = [Reachability reachabilityForInternetConnection]; [internetReachable startNotifIEr]; // check if a pathway to a random host exists hostReachable = [Reachability reachabilityWithHostname:@"www.apple.com"]; [hostReachable startNotifIEr]; // Now patIEntly wait for the notification} 8)设置发送通知的方法,并设置任何检查或调用你可能设置的方法(在我的情况下,我只是设置BOol)
-(voID) checkNetworkStatus:(NSNotification *)notice{ // called after network status changes NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { NSLog(@"The internet is down."); self.internetActive = NO; break; } case ReachableViaWiFi: { NSLog(@"The internet is working via WIFI."); self.internetActive = YES; break; } case ReachableViaWWAN: { NSLog(@"The internet is working via WWAN."); self.internetActive = YES; break; } } NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; switch (hostStatus) { case NotReachable: { NSLog(@"A gateway to the host server is down."); self.hostActive = NO; break; } case ReachableViaWiFi: { NSLog(@"A gateway to the host server is working via WIFI."); self.hostActive = YES; break; } case ReachableViaWWAN: { NSLog(@"A gateway to the host server is working via WWAN."); self.hostActive = YES; break; } }} 9)在你的dealloc或vIEwWilldisappear或类似的方法,删除自己作为观察者
-(voID) vIEwWilldisappear:(BOol)animated{ [[NSNotificationCenter defaultCenter] removeObserver:self];} 注意:可能有一个使用vIEwWilldisappear的实例,其中您收到内存警告,观察者从未获得注册,因此您应该考虑到这一点。
注意:您使用的域不重要。它只是测试到任何域的网关。
重要说明:Reachability类是项目中最常用的类之一,因此您可能会遇到与ShareKit等其他项目的命名冲突。如果发生这种情况,您必须将Reachability.h和Reachability.m文件对中的一个重命名为其他值,以解决该问题。
总结以上是内存溢出为你收集整理的如何检查iOS或OSX上的活动Internet连接?全部内容,希望文章能够帮你解决如何检查iOS或OSX上的活动Internet连接?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)