如何在ios中检查互联网连接? [英] How to check internet connectivity in ios?
问题描述
如何检查应用是否已连接到互联网?
目前,我在 appdelegate.m
文件中使用此代码
How can I check whether the app is connected to the internet or not?
currently, I am using this code in my appdelegate.m
file
dispatch_queue_t connectivityThread = dispatch_queue_create("com.gm.kart.connectivity", NULL);
dispatch_async(connectivityThread, ^{
while (true){
if([GMMConnectivity hasConnectivity])
NSLog(@"%@", @"connected");
else
NSLog(@"Not connected");
usleep(10000000);
}
});
当我点击登录按钮时,我想检查互联网是否已连接使用 NSnotificationcenter
?
and when I click my login button I want to do a check whether the internet is connected or not using NSnotificationcenter
?
请帮帮我
推荐答案
下载后的示例。
http://developer.apple .com / iphone / library / samplecode / Reachability / index.html
您可以在项目中使用它,如下所示: -
you can use it in your Project like bellow steps:-
包括Apple的Reachability.h& .m来自他们的Reachability示例。
添加SystemConfiguration框架。
将bellow方法放入appdelegare.m文件中: -
put bellow method in to your appdelegare.m file:-
- (BOOL) connectedToNetwork{
Reachability* reachability = [Reachability reachabilityWithHostName:@"google.com"];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable)
{
isInternet =NO;
}
else if (remoteHostStatus == ReachableViaWWAN)
{
isInternet = TRUE;
}
else if (remoteHostStatus == ReachableViaWiFi)
{ isInternet = TRUE;
}
return isInternet;
}
isInternet是你的.h类中的BOOL declear
根据您的代码: -
as per your code:-
dispatch_queue_t connectivityThread = dispatch_queue_create("com.GMM.assamkart.connectivity", NULL);
dispatch_async(connectivityThread, ^{
while (true){
isInternet =[self connectedToNetwork];
if (isInternet)
{
NSLog(@"connected");
}
else
{
NSLog(@"Not connected");
}
// usleep(10000000);
}
});
这篇关于如何在ios中检查互联网连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!