检查 iPhone 的网络连接
我正在 iPhone 中开发应用程序,它动态地从网站获取数据,我正在检查 Wifi 和 GPRS 是否已连接,但是当 wifi 无法访问时,应用程序崩溃。
我正在使用这种方法来检查连接。
+ (BOOL) isConnected {
// Part 1 - Create Internet socket addr of zero
struct sockaddr_in zeroAddr;
bzero(&zeroAddr, sizeof(zeroAddr));
zeroAddr.sin_len = sizeof(zeroAddr);
zeroAddr.sin_family = AF_INET;
// Part 2- Create target in format need by SCNetwork
SCNetworkReachabilityRef target =
SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &zeroAddr);
// Part 3 - Get the flags
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(target, &flags);
// Part 4 - Create output
NSString *sNetworkReachable;
if (flags & kSCNetworkFlagsReachable)
sNetworkReachable = @"YES";
else
sNetworkReachable = @"NO";
NSString *sCellNetwork;
if (flags & kSCNetworkReachabilityFlagsIsWWAN)
sCellNetwork = @"YES";
else
sCellNetwork = @"NO";
// Get host entry info for given host
struct hostent *remoteHostEnt = gethostbyname("google.com");
if(remoteHostEnt == nil ) return NO;
// Get address info from host entry
struct in_addr *remoteInAddr = (struct in_addr *) remoteHostEnt->h_addr_list[0];
// Convert numeric addr to ASCII string
char *sRemoteInAddr = inet_ntoa(*remoteInAddr);
NSString *s = [[NSString alloc]
initWithFormat:
@"Network Reachable: %@\n"
@"Cell Network: %@\n"
@"Remote IP: %s\n",
sNetworkReachable,
sCellNetwork,
sRemoteInAddr];
// Add text
[sCellNetwork release];
[sNetworkReachable release];
NSLog(@"Message:%@",s);
return [sNetworkReachable isEqualToString:@"YES"];
}
谢谢 瓦迪韦卢
I am developing app in iPhone which is fetch the data from website dynamically, I am checking whether Wifi and GPRS are connected but when wifi is not reachable, App crash.
I am using this method to check connection.
+ (BOOL) isConnected {
// Part 1 - Create Internet socket addr of zero
struct sockaddr_in zeroAddr;
bzero(&zeroAddr, sizeof(zeroAddr));
zeroAddr.sin_len = sizeof(zeroAddr);
zeroAddr.sin_family = AF_INET;
// Part 2- Create target in format need by SCNetwork
SCNetworkReachabilityRef target =
SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &zeroAddr);
// Part 3 - Get the flags
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(target, &flags);
// Part 4 - Create output
NSString *sNetworkReachable;
if (flags & kSCNetworkFlagsReachable)
sNetworkReachable = @"YES";
else
sNetworkReachable = @"NO";
NSString *sCellNetwork;
if (flags & kSCNetworkReachabilityFlagsIsWWAN)
sCellNetwork = @"YES";
else
sCellNetwork = @"NO";
// Get host entry info for given host
struct hostent *remoteHostEnt = gethostbyname("google.com");
if(remoteHostEnt == nil ) return NO;
// Get address info from host entry
struct in_addr *remoteInAddr = (struct in_addr *) remoteHostEnt->h_addr_list[0];
// Convert numeric addr to ASCII string
char *sRemoteInAddr = inet_ntoa(*remoteInAddr);
NSString *s = [[NSString alloc]
initWithFormat:
@"Network Reachable: %@\n"
@"Cell Network: %@\n"
@"Remote IP: %s\n",
sNetworkReachable,
sCellNetwork,
sRemoteInAddr];
// Add text
[sCellNetwork release];
[sNetworkReachable release];
NSLog(@"Message:%@",s);
return [sNetworkReachable isEqualToString:@"YES"];
}
Thanks
Vadivelu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么要搞乱 IP 地址?您可以直接调用
SCNetworkReachabilityCreateWithName()
:Why mess with IP addresses? You can call
SCNetworkReachabilityCreateWithName()
directly instead:在 developer.apple.com 上搜索 Reachability 示例应用程序,
还有开源 NPReachability 类,它执行相同的操作,但使用块作为处理程序。
Search for the Reachability sample application on developer.apple.com
There is also the Open Source NPReachability class on GitHub which does the same thing but uses a block as a handler.
您可以使用隐藏的 UIWebView 并向其加载网站。然后使用 UIWebView.h 中的方法来检测加载是否成功。
You could use a hidden UIWebView and load a website to it. Then use methods from the UIWebView.h to detect, whether loading was successful or not.