zoukankan      html  css  js  c++  java
  • iOS网络检测

    使用之前请从Apple网站下载示例:点此下载

    Reachability 中定义了3种网络状态:

    typedef enum : NSInteger {
        NotReachable = 0,//无网络
        ReachableViaWiFi,//wifi
        ReachableViaWWAN//3g
    } NetworkStatus;

    比如检测某一特定站点的接续状况,可以使用下面的代码:

        Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
        switch ([r currentReachabilityStatus]) {
            case NotReachable:
                // 没有网络连接
                NSLog(@"没有网络连接");
                break;
            case ReachableViaWWAN:
                NSLog(@"使用3g网络");
                // 使用3G网络
                break;
            case ReachableViaWiFi:
                NSLog(@"使用wifi网络");
                // 使用WiFi网络
                break;
        }

    检测当前网络环境:

    // 是否wifi
    + (BOOL) IsEnableWIFI {
        return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
    }
    
    // 是否3G
    + (BOOL) IsEnable3G {
        return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
    }

    连接状态实时通知:

    #import <UIKit/UIKit.h>
    #import "Reachability.h"
    @interface AppDelegate : UIResponder  <UIApplicationDelegate> {
        Reachability  *hostReach;
    }
    @property (strong, nonatomic) UIWindow *window;
    
    
    @end
    - (void)reachabilityChanged:(NSNotification *)note {
        // 连接改变
        Reachability* curReach = [note object];
        NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
        //处理连接改变后的情况
        NetworkStatus status = [curReach currentReachabilityStatus];
        
        if (status == NotReachable) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"你的网络已断开连接"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
    }
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // 监测网络情况
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(reachabilityChanged:)
                                                     name: kReachabilityChangedNotification
                                                   object: nil];
        hostReach = [Reachability reachabilityWithHostName:@"www.google.com"];//可以以多种形式初始化
        [hostReach startNotifier];//开始监听,会启动一个run loop
        return YES;
    }
     
  • 相关阅读:
    插件有感-做东西有感
    国家电网-元数据管理系统-流程跳转有感-3层结构
    干了2个月java开发最深的体会
    BL老师的建议,数学不好的,大数据一票否决--后赋从java转大数据
    极快瑞的函数式编程,Jquery涉及的一些函数
    距离第一天去实习过去了56天 ::写写自己的想法
    两种同步模式:状态同步和帧同步
    unity制作人物残影-绘制的方法
    unity对敏感词库处理的记录
    unity 读取外部exe程序控制台信息
  • 原文地址:https://www.cnblogs.com/hxwj/p/4461885.html
Copyright © 2011-2022 走看看