有没有其他方法来检查 iPhone 中的网络可用性而不是服务可达性?
我正在创建一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法(也是我经常使用的方法)是从服务器请求资源。请务必禁止任何缓存,并检查它是否返回结果。
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.