判断网络连接情况

来源:互联网 发布:淘宝网iphone6s官换机 编辑:程序博客网 时间:2024/05/16 01:26

判断网络是否可用


1. 从官网例子Reachability中copy Reachability.h和Reachability.m

2. 添加Systemconfiguration.framework

3. 使用下列方法

Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];

    switch ([r currentReachabilityStatus]) {

        case NotReachable:

            // 没有网络连接

            NSLog(@"没有网络");

    

            break;

        case ReachableViaWWAN:

            // 使用3G网络

            NSLog(@"正在使用3G网络");

            break;

        case ReachableViaWiFi:

            // 使用WiFi网络

            NSLog(@"正在使用wifi网络");        

            break;

    }



warning: declaration of 'struct sockaddr_in' will not be visible outside of this function [3]

 + (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;


解决:在Reachability.h中添加#import<netinet/in.h>// sockaddr_in

原创粉丝点击