Reachability实时网络监听/AFNETWorking2.0+ 网络监听

来源:互联网 发布:网络公安报警 编辑:程序博客网 时间:2024/05/21 17:59
下载 官方 Reachability demo 导入Reachability.h .m 文件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];        //开启网络状况的监听    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];        self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;    [self.hostReach startNotifier];  //开始监听,会启动一个run loop    self.window.rootViewController = self.tabBarController;    [self.window makeKeyAndVisible];    return YES;}//网络链接改变时会调用的方法-(void)reachabilityChanged:(NSNotification *)note{    Reachability *currReach = [note object];    NSParameterAssert([currReach isKindOfClass:[Reachability class]]);        //对连接改变做出响应处理动作    NetworkStatus status = [currReach currentReachabilityStatus];    //如果没有连接到网络就弹出提醒实况    self.isReachable = YES;    if(status == NotReachable)    {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:@"暂无法访问书城信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];        [alert show];        [alert release];        self.isReachable = NO;    }    else    {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接信息" message:@"网络连接正常" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];        [alert show];        [alert release];        self.isReachable = YES;    }}
AFNETWorking2.0+ 网络监听
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(83, 154, 164);"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">- (<span style="color: rgb(195, 34, 117);">BOOL</span>)application:(<span style="color: rgb(97, 34, 174);">UIApplication</span> *)application didFinishLaunchingWithOptions:(<span style="color: rgb(97, 34, 174);">NSDictionary</span> *)launchOptions</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">{</p><p></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(83, 154, 164);"><span style="color: rgb(0, 0, 0);"><span style="white-space: pre;"></span>[[</span><span style="color: rgb(97, 34, 174);">NSNotificationCenter</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(61, 29, 129);">defaultCenter</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(61, 29, 129);">addObserver</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(195, 34, 117);">self</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(61, 29, 129);">selector</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(195, 34, 117);">@selector</span><span style="color: rgb(0, 0, 0);">(reachabilityChanged:)<span style="white-space: pre;"></span></span><span style="color: rgb(61, 29, 129);">name</span><span style="color: rgb(0, 0, 0);">:</span>AFNetworkingReachabilityDidChangeNotification<span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(61, 29, 129);">object</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(195, 34, 117);">nil</span><span style="color: rgb(0, 0, 0);">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(60, 130, 140);"><span style="white-space: pre;"></span>AFNetworkReachabilityManager<span style="color: rgb(0, 0, 0);"> *manager = [</span>AFNetworkReachabilityManager<span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(41, 76, 80);">sharedManager</span><span style="color: rgb(0, 0, 0);">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">    <span style="white-space: pre;"></span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(60, 130, 140);"><span style="color: rgb(0, 0, 0);"><span style="white-space: pre;"></span>[</span>AFNetworkReachabilityManager<span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(41, 76, 80);">managerForDomain</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(201, 27, 19);">@"www.baidu.com"</span><span style="color: rgb(0, 0, 0);">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(41, 76, 80);"><span style="color: rgb(0, 0, 0);"><span style="white-space: pre;"></span>[manager </span>startMonitoring<span style="color: rgb(0, 0, 0);">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(41, 76, 80);"><span style="color: rgb(0, 0, 0);"></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(41, 76, 80);"><span style="color: rgb(0, 0, 0);">}</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(41, 76, 80);"><span style="color: rgb(0, 0, 0);"></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(41, 76, 80);"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">-(<span style="color: rgb(195, 34, 117);">void</span>)reachabilityChanged:(<span style="color: rgb(60, 130, 140);">AFNetworkReachabilityManager</span> *)note</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(60, 130, 140);"><span style="color: rgb(0, 0, 0);">    </span>AFNetworkReachabilityManager<span style="color: rgb(0, 0, 0);"> *manager = [</span>AFNetworkReachabilityManager<span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(41, 76, 80);">sharedManager</span><span style="color: rgb(0, 0, 0);">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; color: rgb(49, 89, 93);"><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(195, 34, 117);">if</span><span style="color: rgb(0, 0, 0);"> (manager.</span><span style="color: rgb(83, 154, 164);">networkReachabilityStatus</span><span style="color: rgb(0, 0, 0);"> == </span>AFNetworkReachabilityStatusNotReachable<span style="color: rgb(0, 0, 0);">) {</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">        <span style="color: rgb(97, 34, 174);">UIAlertView</span> *alert = [[<span style="color: rgb(97, 34, 174);">UIAlertView</span> <span style="color: rgb(61, 29, 129);">alloc</span>]<span style="color: rgb(61, 29, 129);">initWithTitle</span>:<span style="color: rgb(195, 34, 117);">nil</span> <span style="color: rgb(61, 29, 129);">message</span>:<span style="color: rgb(201, 27, 19);">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(201, 27, 19);">没有网络链接</span><span style="color: rgb(201, 27, 19);"> "</span> delegate:<span style="color: rgb(195, 34, 117);">nil</span><span style="color: rgb(61, 29, 129);">cancelButtonTitle</span>:<span style="color: rgb(201, 27, 19);">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(201, 27, 19);">我知道了</span><span style="color: rgb(201, 27, 19);">"</span> otherButtonTitles: <span style="color: rgb(195, 34, 117);">nil</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">        [alert <span style="color: rgb(61, 29, 129);">show</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">    }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo; min-height: 16px;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">}</p><p></p>
                                             
0 0
原创粉丝点击