iOS无法访问互联网

时间:2015-12-08 16:09:57

标签: ios reachability

我正在检查我的应用是否可以访问互联网。奇怪的是,在某些设备上,我可以访问互联网。但是,某些设备不合适。我的编码是否正确?

如果是这样,是因为细胞数据吗?它只发生在我的应用程序和所有其他应用程序似乎没关系。在蜂窝设置中,它允许我的应用程序使用蜂窝数据。

+ (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)isReachableWithoutRequiringConnection:(SCNetworkReachabilityFlags)flags
{
    // kSCNetworkReachabilityFlagsReachable indicates that the specified nodename or address can
// be reached using the current network configuration.
BOOL isReachable = flags & kSCNetworkReachabilityFlagsReachable;

// This flag indicates that the specified nodename or address can
// be reached using the current network configuration, but a
// connection must first be established.
//
// If the flag is false, we don't have a connection. But because CFNetwork
// automatically attempts to bring up a WWAN connection, if the WWAN reachability
// flag is present, a connection is not required.
    BOOL noConnectionRequired = !(flags & kSCNetworkReachabilityFlagsConnectionRequired);
        if ((flags & kSCNetworkReachabilityFlagsIsWWAN)) {
        noConnectionRequired = YES;
    }

    return (isReachable && noConnectionRequired) ? YES : NO;
}

1 个答案:

答案 0 :(得分:0)

某些设备无法访问互联网是正常的。 WiFi关闭,3G关闭,飞行模式,连接不良,路由器拔掉。或者它是没有任何3G的iPad。所有可能的。

一旦您的应用程序正常运行,您就需要对互联网开启和关闭时发生的情况进行一些认真的测试。例如,当您离开办公室并且WiFi消失时会发生什么。或者您步行穿过购物中心,各种WiFis来去匆匆。或者用户更改设置。你的应用程序应该存活下来