ios检测是否有网络连接

去这里http://developer.apple.com/iphone/library/samplecode/Reachability/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007324-Intro-DontLinkElementID_2 下载Reachability.m和Reachability.h,然后添加到你的项目里面,在需要测试网络连接class里面写这样的一个方法

- (BOOL)testConnection {
    BOOL result = YES;
    Reachability *reach=[Reachability sharedReachability];
    [reach setHostName:@“你自己输入一个网址来做ping”];
    NetworkStatus status;
    status=[reach remoteHostStatus];
    if (status == NotReachable) {
        result = NO;
    } else if (status == ReachableViaCarrierDataNetwork) {
        result = YES;
    } else if (status == ReachableViaWiFiNetwork) {
        result = YES;
    }else {
        result = NO;
    }
    return result;
}

posted @ 2012-09-19 17:34  hellocby  阅读(1159)  评论(0编辑  收藏  举报