How to check internet connectivity in ios?

对着背影说爱祢 提交于 2019-12-03 21:51:55

After download bellow example.

http://developer.apple.com/iphone/library/samplecode/Reachability/index.html

you can use it in your Project like bellow steps:-

included Apple's Reachability.h & .m from their Reachability example.

add the SystemConfiguration framework.

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 is a BOOL declear in to your .h class

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);
    }
});
AP_
-(BOOL) connectedToInternet

{
    NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];

return ( URLString != NULL ) ? YES : NO;

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!