可达性 - 显示webView之前的最佳实践 [英] Reachability - Best practice before showing webView
问题描述
我想在我的应用程序中显示webView之前/之后检查可达性。
I would like to check "reachability" before/when a webView is shown in my app.
到目前为止,我已经包含了reachbility.h / .m文件以及将SystemConfiguration.framework添加到项目中。
So far I've included the reachbility.h/.m files as well as added the SystemConfiguration.framework to the project.
这是关于我在互联网上找到的协议的终点,来自所有帖子和博客等等等。每个人对此之后要做的事情有不同的想法/意见。另外,我发现很多部分代码片段并不是真正完整的解决方案,可调用的方法等等等等。
And that's about where the agreements I have found on the internet end, from all of the posts and blogs etc. etc. everyone has a different idea/opinion of what to do after that. Additionally, I have found a lot partial code snippets that aren't really a complete solution, on which reachibility methods to call etc. etc. how to use them etc.
我还发现有些人警告你应该在检查可达性之前尝试重新连接......但我还没有找到共识或完整的解决方案。我的应用程序似乎重新连接到wifi而没有任何额外的代码...所以我也很困惑这里...
I have also found that some warn that you should try to reconnect before checking reachability...but I haven't found a consensus or a full solution. My app seems to reconnect to wifi without any extra code... so I'm a litte confused here too...
任何帮助清除泥泞的水域将是赞赏。我只是在寻找一个简单直接的解决方案。
Any help to clear the muddy waters would be appreciated. I'm just looking for a simple straightforward solution.
接受的答案:我想向那些可能会读到这个问题的新手注意......你会想要执行以下操作:
Answer Accepted: I would like to note to newbies who may read this q/a later... that you will want to do the following:
将此添加到.h文件中:
Add this into your .h file:
- (BOOL) connectedToNetwork: (NSString *) remoteServer;
- (void) appLoadError: (NSString *) altertTitle alertMessage: (NSString *) altertMsg;
您需要在.m文件的顶部导入这些:
And you will need to import these at the top of your .m file:
sys / socket.h
sys/socket.h
netinet / in.h
netinet/in.h
netinet6 / in6.h
netinet6/in6.h
arpa / inet.h
arpa/inet.h
ifaddrs.h
ifaddrs.h
netdb.h
SystemConfiguration / SystemConfiguration.h
SystemConfiguration/SystemConfiguration.h
如果错误,请纠正我......对我来说似乎工作正常......
Correct me if it is wrong... It seems to work fine for me...
推荐答案
当我需要访问Internet时,我总是在我的App Delegate中使用此方法。我随着时间的推移调整了不同的访问类型,它对我很有帮助。这是您在快速搜索此主题后可以找到的众多方法之一的变体。
I have always used this method in my App Delegate when I need to require Internet access. I have tuned it for different access types over time and it has served me well. It is a variant of one of the many methods you can find after a quick Google search on this topic.
提出一个快速和快速的方法是一件棘手的事情策略围绕这个。平台本身提供不同的连接选项,根据每个特定应用程序的需要具有优缺点。我在下面使用的方法只是一般的连接测试,这意味着设备可以通过某种连接机制到达Internet。
It is a tricky thing to come up with a hard and fast strategy around this. The platform itself offers different connectivity options that have pros and cons based on the needs of each specific application. The method I use below is just a general connectivity test meaning the device can reach the Internet via some connectivity mechanism.
- (BOOL) connectedToNetwork: (NSString *) remoteServer {
// Create zero addy
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags){
NSLog(@"Error. Could not recover network reachability flags");
return NO;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
NSURL *testURL = [NSURL URLWithString: remoteServer];
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
}
我通常在applicationDidFinishLaunching中调用它。如果检查返回false,我通常会生成错误消息(见下文)。这是因为Apple禁止通过设备上的主页按钮以外的任何方式退出应用程序。
I usually call this inside applicationDidFinishLaunching. If the check returns false, I usually generate an error message (see below). This is because Apple forbids exiting the application by any means other than the home button on the device.
...
else if(![self connectedToNetwork: [NSString stringWithFormat: @"http://%@:%@/", sharedSettings.server, sharedSettings.port]]){
[self appLoadError: @"No Internet Detected" alertMessage:@"This application requires an active Internet connection. No content is available."];
}
...
- (void) appLoadError: (NSString *) altertTitle alertMessage: (NSString *) altertMsg {
UIAlertView *loadErr = [[UIAlertView alloc] initWithTitle: altertTitle message: altertMsg delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil];
[loadErr show];
[loadErr release];
// Load static screen
UIImage *image = [UIImage imageNamed:@"Error_NoInternet.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[controller setNavigationBarHidden: YES animated: NO];
[window addSubview: imageView];
[imageView release];
}
这篇关于可达性 - 显示webView之前的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!