如何在iPhone中检查互联网状态 [英] How to check Internet status in iPhone

查看:60
本文介绍了如何在iPhone中检查互联网状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查是否使用SystemConfiguration或CFNetwork连接了Internet,但我不确定哪一个.

I wanted to check whether internet is connected or not using either the SystemConfiguration or the CFNetwork i am not quite sure which one.

那我想知道如果互联网已连接,那么它是否通过wifi连接.

Then i want to know that if the internet is connected then is it connected through wifi or not.

我尝试了一个使用以下代码的示例

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";
    }

}


有人说它对每个苹果都无效,您必须使用SystemConfiguration Framework,请让我知道需要做什么.

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.

我个人也认为我在上面的代码中所做的事情是不合适的,好像有一天Google可能由于维护或其他一些因素而倒闭了.

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.

如果您能给我提供一个可以显示WIFI网络名称的链接,那真是太酷了.

Also if you could provide me a link where i could display the name of the WIFI network then it would be really cool.

我搜索了互联网,然后得到了这些Reachability.h代码,因为我想学习不复制的概念,所以它们再次成为弹跳器

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

感谢和问候

推荐答案

在Raxit提及的基础上,我使用以下代码(从Raxit提及的可达性示例中提取)来检查我的应用程序委托中的Internet访问:

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:

- (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"];
}

这篇关于如何在iPhone中检查互联网状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆