zoukankan      html  css  js  c++  java
  • iOS中判断网络是否联网

    #import "AppDelegate.h"
    
    #import "ViewController.h"
    #import "Reachability.h"
    
    
    @interface AppDelegate (){
    }
    @property (nonatomic,retain)Reachability *res;
    
    @end
    
    @implementation AppDelegate
    
    - (void)dealloc
    {
        [_window release];
        [_res release];
        [_viewController release];
        [super dealloc];
    }
    
    -(void)change:(NSNotificationCenter *)notify{//当网络发生变化的时候,都会触发这个事件
        NSLog(@"%@",notify);
    }
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        
    
    //添加网络变化的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:kReachabilityChangedNotification object:nil];
        self.res=[Reachability reachabilityWithHostName:@"http://www.baidu.com"];
        [self.res startNotifier];//开始监听网络请求的变化
        
        
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        // Override point for customization after application launch.
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    、-(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        if([Reachability reachabilityForLocalWiFi].currentReachabilityStatus==NotReachable&&[[Reachability reachabilityForInternetConnection] currentReachabilityStatus]==NotReachable){
            NSLog(@" not  work");
        }
     
    }
    
    -(void)PdNetwork{
        Reachability *r=[Reachability reachabilityWithHostName:@"http://www.baidu.com"];
        switch ([r currentReachabilityStatus]) {
            case NotReachable:
                NSLog(@"not work");
                break;
            case ReachableViaWiFi:
                NSLog(@"wifi");
                break;
            case ReachableViaWWAN:{
                NSLog(@"wan");
            }
                break;
            default:
                break;
        }
    }
  • 相关阅读:
    清除某个数据库的所有数据库连接的存储过程
    IIS的Windows集成身份验证总结
    新项目的页面不要直接从PageBase继承
    安装Win2003 SP1遇到拒绝访问
    ASP.NET2.0站点跨服务器访问Sql Sever 2005 Reporting Service
    当CodeSmith不在时,续……
    Web讯雷导致IIS无法启动的问题
    Intro to eDiscovery in SharePoint, Exchange, and Lync 2013
    微软云平台
    团队开发博客
  • 原文地址:https://www.cnblogs.com/gcb999/p/3175524.html
Copyright © 2011-2022 走看看