如何在ios中检查互联网连接?

Nav*_*een 0 notifications objective-c nsnotificationcenter ios

如何检查应用程序是否已连接到互联网?目前,我在我的appdelegate.m文件中使用此代码

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);
    }
});
Run Code Online (Sandbox Code Playgroud)

当我点击我的登录按钮时,我想检查互联网是否已连接NSnotificationcenter

请帮我

Nit*_*hel 11

下载后的示例.

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

您可以在项目中使用它,如下面的步骤: -

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

add the SystemConfiguration framework.

将波纹管方法放入appdelegare.m文件: -

- (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;
}
Run Code Online (Sandbox Code Playgroud)

isInternet是一个BOOL declear到你的.h类

根据你的代码: -

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);
    }
});
Run Code Online (Sandbox Code Playgroud)