[英]How to check Internet status in iPhone
I wanted to check whether internet is connected or not using either the SystemConfiguration or the CFNetwork i am not quite sure which one. 我想检查是否使用SystemConfiguration或CFNetwork连接了Internet,但我不确定哪一个。
Then i want to know that if the internet is connected then is it connected through wifi or not. 然后我想知道,如果互联网已连接,那么它是否通过wifi连接。
I tried an example where i used the below code 我尝试了一个使用以下代码的示例
-(IBAction) shownetworkStatus { NSString *str = @"http://www.google.com"; NSURL *url = [NSURL URLWithString:str]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; if (req!=NULL) { lbl.text = @"Connected"; } else { lbl.text = @"notConnected"; } }
some say that its not valid as per apple and you have to use the SystemConfiguration Framework, Please let me know what needs to be done. 有人说它对每个苹果都无效,您必须使用SystemConfiguration Framework,请让我知道需要做什么。
Also i personally think that what i am doing in the above code is not proper as if one day google may also be down due to maintenance or some other factors. 我个人也认为我在上面的代码中所做的事情是不合适的,好像有一天Google也可能由于维护或其他一些因素而倒闭了。
Also if you could provide me a link where i could display the name of the WIFI network then it would be really cool. 另外,如果您可以为我提供一个可以显示WIFI网络名称的链接,那真的很酷。
I searched the internet then i got these Reachability.h code which again is a bouncer as i wana learn the concepts not copy paste them 我搜索了互联网,然后得到了这些Reachability.h代码,因为我要学习不复制的概念,所以它们又是一个保镖。
Thanks and Regards 谢谢并恭祝安康
Building on top of what Raxit mentions, I use the following code (extracted from the reachability example mentioned by Raxit) to check for internet access in my application delegate: 在Raxit提到的内容的基础上,我使用以下代码(从Raxit提到的可达性示例中提取)来检查我的应用程序委托中的Internet访问:
- (BOOL)isReachableWithoutRequiringConnection:(SCNetworkReachabilityFlags)flags
{
BOOL isReachable = flags & kSCNetworkReachabilityFlagsReachable;
BOOL noConnectionRequired = !(flags & kSCNetworkReachabilityFlagsConnectionRequired);
if ((flags & kSCNetworkReachabilityFlagsIsWWAN)) {
noConnectionRequired = YES;
}
return (isReachable && noConnectionRequired) ? YES : NO;
}
- (BOOL)isHostReachable:(NSString *)host
{
if (!host || ![host length]) {
return NO;
}
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [host UTF8String]);
BOOL gotFlags = SCNetworkReachabilityGetFlags(reachability, &flags);
CFRelease(reachability);
if (!gotFlags) {
return NO;
}
return [self isReachableWithoutRequiringConnection:flags];
}
- (BOOL)connectedToNetwork {
return [self isHostReachable:@"www.hostyoureallycareabouthavingaconnectionwith.com"];
}
You can use the reachability code provided by Apple. 您可以使用Apple提供的可达性代码。 It is sample code.
它是示例代码。 you can find whole detail regarding internet connection.
您可以找到有关互联网连接的全部详细信息。 This is the link http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
这是链接http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.