有没有其他方法来检查 iPhone 中的网络可用性而不是服务可达性?

发布于 2024-09-11 21:57:58 字数 1217 浏览 1 评论 0原文

我正在创建一个简单的 iPhone 应用程序,我需要检查互联网可用性。为了检查这一点,论坛建议使用可达性类。基于此,我实现了以下代码。

struct sockaddr_in zeroAddress;  
bzero(&zeroAddress, sizeof(zeroAddress));  
zeroAddress.sin_len = sizeof(zeroAddress);  
zeroAddress.sin_family = AF_INET;  

SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);  
SCNetworkReachabilityFlags flags;  
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);  
CFRelease(defaultRouteReachability); 

if (!didRetrieveFlags) {   
    isInternetConnectionAvailable = NO;  
}  

BOOL isReachable = flags & kSCNetworkFlagsReachable;  
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; 

NSURL *testURL = [NSURL URLWithString:serviceEndPoint];  
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];  
[[[NSURLConnection alloc] initWithRequest:testRequest delegate:self] autorelease];  

但是,我希望有一种简单的替代方法来获取网络状态。在 iPhone 中,它会显示网络状态本身,如果我可以重用它,那么可以消除通过我的应用程序检查可用性的开销。我找到了一些通过通知获取它的新方法,但仍然无法获得更多详细信息。

如果有人遇到一个简单的方法来完成此任务,请与我分享。

I'm creating a simple iphone application and there I need to check the internet availability. To check this most the forums recommended to use the reachability class. Based on that I have implemented the following code.

struct sockaddr_in zeroAddress;  
bzero(&zeroAddress, sizeof(zeroAddress));  
zeroAddress.sin_len = sizeof(zeroAddress);  
zeroAddress.sin_family = AF_INET;  

SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);  
SCNetworkReachabilityFlags flags;  
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);  
CFRelease(defaultRouteReachability); 

if (!didRetrieveFlags) {   
    isInternetConnectionAvailable = NO;  
}  

BOOL isReachable = flags & kSCNetworkFlagsReachable;  
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired; 

NSURL *testURL = [NSURL URLWithString:serviceEndPoint];  
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];  
[[[NSURLConnection alloc] initWithRequest:testRequest delegate:self] autorelease];  

However, I wish to have a simple alternative method to get the network status. In iphone it displays the network status itself,if i can reuse it then the overhead of checking the availability through my application can be eliminated. I found some new way of getting it through notifications, but still I couldn't get more details about it.

If anyone come across with a simple method to get it done this please share with me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2024-09-18 21:57:58

最简单的方法(也是我经常使用的方法)是从服务器请求资源。请务必禁止任何缓存,并检查它是否返回结果。

The most easiest way (and the one I always use) is to just request a resource from the server. Be sure to disallow any caching, and check if it comes back with a result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文