检查Internet连接

时间:2011-03-09 14:49:06

标签: iphone objective-c connection

我已经尝试检查是否存在与google.com的连接 但是这种方法不能正常工作? 你能告诉我我的错吗? 你的拉斐尔

- (BOOL)networkConnected {
    SCNetworkReachabilityFlags flags = 0;
    SCNetworkReachabilityRef netReachability;
    BOOL retrievedFlags = NO;
    netReachability = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [@"http://www.google.com" UTF8String]);

    if (netReachability) {
        retrievedFlags = SCNetworkReachabilityGetFlags(netReachability, &flags);
        CFRelease(netReachability);
    }
    if (!retrievedFlags || !flags) {
        NSLog(@"no");
        return NO;
    }
    NSLog(@"yes");
    return YES;
} 

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

您可以在Objective-c中使用iPhone吗?

DWORD flags, reserved;
flags = reserved = 0;
if(!InternetGetConnectedState(&flags, reserved))
{
    // No internet connection
} else {
    // Have internet connection
}