封装iphone网络管理,用户只需要实现代理即可,欢迎使用。

有需要的同学可以下载借鉴。

 

使用之前需要将apple的 Reachability.h 和 Reachability.m文件拷贝到工程中去。

 

下面是类NetWorkManager的实现。

 

NetWorkManager.h

 

// // NetWorkManager.h // Constellation // // Created by jeffrey on 10-12-5. // Copyright 2010 AppUFO. All rights reserved. #import <Foundation/Foundation.h> #import "Reachability.h" #pragma mark NetWorkManagerDelegate /** * * 您的应用程序需要实现此协议,当网络发生变化时候,与用户交互 * */ @protocol NetWorkManagerDelegate @required - (void) netWorkStatusWillChange:(NetworkStatus)status; @optional - (void) netWorkStatusWillEnabled; - (void) netWorkStatusWillEnabledViaWifi; - (void) netWorkStatusWillEnabledViaWWAN; - (void) netWorkStatusWillDisconnection; @end #pragma mark NetWorkManager @interface NetWorkManager : NSObject<UIApplicationDelegate> { @private Reachability* rech; /** 标识网络是否活跃 **/ Boolean _netWorkIsEnabled; /** 设备链接网络的方式 **/ NetworkStatus _witchNetWorkerEnabled; /** 代理 **/ id<NetWorkManagerDelegate> delegate; } ///!!!NOTICE:WNEH YOU WANT TO GET THIS,YOU MUST START THE WATCH FIRST @property (readonly, getter = witchNetWorkerEnabled) NetworkStatus _witchNetWorkerEnabled; ///!!!NOTICE:WNEH YOU WANT TO GET THIS,YOU MUST START THE WATCH FIRST @property (readonly, getter = netWorkIsEnabled) Boolean _netWorkIsEnabled; @property (nonatomic, retain) id<NetWorkManagerDelegate> delegate; /** * * 获取网络管理器 * */ + (id) sharedManager; /** * * 防止以其他方法创建第二实例 * */ + (id) allocWithZone:(NSZone *)zone; /** * * 检测当前网络状态 * */ - (NetworkStatus) checkNowNetWorkStatus; /** * * 开始检测网络 * */ - (Boolean) startNetWorkeWatch; /** * * 停止检测网络 * */ - (void) stopNetWorkWatch; @end #pragma mark NetWorkManagerPrivateMethod @interface NetWorkManager(private) /** * * 当网络发生变化时 * */ - (void)reachabilityChanged:(NSNotification *)note; @end  

 

NetWorkManager.m

 

// // NetWorkManager.m // Constellation // // Created by jeffrey on 10-12-5. // Copyright 2010 AppUFO. All rights reserved. // #import "NetWorkManager.h" @implementation NetWorkManager @synthesize _netWorkIsEnabled; @synthesize _witchNetWorkerEnabled; @synthesize delegate; static NetWorkManager* defaultManager = nil; + (NetWorkManager*) sharedManager { if (!defaultManager) { defaultManager = [[self alloc] init]; } return defaultManager; } + (id) allocWithZone:(NSZone *)zone { @synchronized(self) { if (defaultManager == nil) { defaultManager = [super allocWithZone:zone]; return defaultManager; } } return nil; } - (NetworkStatus) checkNowNetWorkStatus { Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"]; return [r currentReachabilityStatus]; } - (Boolean) startNetWorkeWatch { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil]; rech = [[Reachability reachabilityWithHostName:@"www.google.com"] retain]; Boolean finish = [rech startNotifier]; return finish; } - (void) stopNetWorkWatch { [rech stopNotifier]; [[NSNotificationCenter defaultCenter] removeObserver:self]; [defaultManager release]; defaultManager = nil; } - (void)reachabilityChanged:(NSNotification *)note { Reachability* curReach = [note object]; NSParameterAssert([curReach isKindOfClass: [Reachability class]]); NetworkStatus status = [curReach currentReachabilityStatus]; //调用代理方法,此方法为必须实现 [delegate netWorkStatusWillChange:status]; //代理的可选方法 switch (status) { case NotReachable: { //网络不可达 _netWorkIsEnabled = NO; _witchNetWorkerEnabled = NotReachable; if ([(NSObject*)delegate respondsToSelector:@selector(netWorkStatusWillDisconnection)]) { [delegate netWorkStatusWillDisconnection]; } } break; case ReachableViaWiFi: { //网络可达 _netWorkIsEnabled = YES; _witchNetWorkerEnabled = ReachableViaWiFi; if ([(NSObject*)delegate respondsToSelector:@selector(netWorkStatusWillEnabledViaWifi)]) { [delegate netWorkStatusWillEnabledViaWifi]; } } break; case ReachableViaWWAN: { //网络可达 _netWorkIsEnabled = YES; _witchNetWorkerEnabled = ReachableViaWWAN; if ([(NSObject*)delegate respondsToSelector:@selector(netWorkStatusWillEnabledViaWWAN)]) { [delegate netWorkStatusWillEnabledViaWWAN]; } } break; default: break; } } - (void) dealloc { if (defaultManager) { [defaultManager release]; } if (delegate) { [(NSObject*)delegate release]; } [super dealloc]; } @end  

 

 

 

NetWorkManager是以单例的模式提供的,用户实现NetWorkMangerDelegate的代理方法,就可以实时的

 

收到网络变化通知,然后定制功能。

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #ed1d13}

你可能感兴趣的:(封装iphone网络管理,用户只需要实现代理即可,欢迎使用。)