IOS判断网络环境
https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
我下载的是vertion2.2
开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审查的。
Reachability
Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。
在你的程序中使用
1、Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中; 2、然后将 SystemConfiguration.framework 添加进工程。
Reachability 中定义了3种网络状态。
typedefenum {
NotReachable = 0,//无连接
ReachableViaWiFi,//使用3G/GPRS网络
ReachableViaWWAN//使用WiFi网络
} NetworkStatus;
简单判断项目连接:
// 是否wifi
+ (BOOL) IsEnableWIFI {
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
// 是否网络连接
+ (BOOL) IsEnableConnection {
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
// 是否网络连接
+ (BOOL) IsEnable3G {
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWWAN);
}
下面是监听整个项目网络例子:
- /*
- File: ReachabilityAppDelegate.m
- Abstract: The application's controller.
- Version: 2.2
- Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
- ("Apple") in consideration of your agreement to the following terms, and your
- use, installation, modification or redistribution of this Apple software
- constitutes acceptance of these terms. If you do not agree with these terms,
- please do not use, install, modify or redistribute this Apple software.
- In consideration of your agreement to abide by the following terms, and subject
- to these terms, Apple grants you a personal, non-exclusive license, under
- Apple's copyrights in this original Apple software (the "Apple Software"), to
- use, reproduce, modify and redistribute the Apple Software, with or without
- modifications, in source and/or binary forms; provided that if you redistribute
- the Apple Software in its entirety and without modifications, you must retain
- this notice and the following text and disclaimers in all such redistributions
- of the Apple Software.
- Neither the name, trademarks, service marks or logos of Apple Inc. may be used
- to endorse or promote products derived from the Apple Software without specific
- prior written permission from Apple. Except as expressly stated in this notice,
- no other rights or licenses, express or implied, are granted by Apple herein,
- including but not limited to any patent rights that may be infringed by your
- derivative works or by other works in which the Apple Software may be
- incorporated.
- The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
- WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
- WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
- COMBINATION WITH YOUR PRODUCTS.
- IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
- DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
- CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
- APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- Copyright (C) 2010 Apple Inc. All Rights Reserved.
- */
- #import "ReachabilityAppDelegate.h"
- #import "Reachability.h"
- @implementation ReachabilityAppDelegate
- - (void) configureTextField: (UITextField*) textField imageView: (UIImageView*) imageView reachability: (Reachability*) curReach
- {
- NetworkStatus netStatus = [curReach currentReachabilityStatus];
- BOOL connectionRequired= [curReach connectionRequired];
- NSString* statusString= @"";
- switch (netStatus)
- {
- case NotReachable:
- {
- statusString = @"Access Not Available";
- imageView.image = [UIImage imageNamed: @"stop-32.png"] ;
- //Minor interface detail- connectionRequired may return yes, even when the host is unreachable. We cover that up here...
- connectionRequired= NO;
- break;
- }
- case ReachableViaWWAN:
- {
- statusString = @"Reachable WWAN";
- imageView.image = [UIImage imageNamed: @"WWAN5.png"];
- break;
- }
- case ReachableViaWiFi:
- {
- statusString= @"Reachable WiFi";
- imageView.image = [UIImage imageNamed: @"Airport.png"];
- break;
- }
- }
- if(connectionRequired)
- {
- statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];
- }
- textField.text= statusString;
- }
- - (void) updateInterfaceWithReachability: (Reachability*) curReach
- {
- if(curReach == hostReach)
- {
- [self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];
- NetworkStatus netStatus = [curReach currentReachabilityStatus];
- BOOL connectionRequired= [curReach connectionRequired];
- summaryLabel.hidden =(netStatus != ReachableViaWWAN);
- NSString* baseLabel= @"";
- if(connectionRequired)
- {
- baseLabel= @"Cellular data network is available.\n Internet traffic will be routed through it after a connection is established.";
- }
- else
- {
- baseLabel= @"Cellular data network is active.\n Internet traffic will be routed through it.";
- }
- summaryLabel.text= baseLabel;
- }
- if(curReach == internetReach)
- {
- [self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];
- }
- if(curReach == wifiReach)
- {
- [self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];
- }
- }
- //Called by Reachability whenever status changes.
- - (void) reachabilityChanged: (NSNotification* )note
- {
- Reachability* curReach = [note object];
- NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
- [self updateInterfaceWithReachability: curReach];
- }
- - (void) applicationDidFinishLaunching: (UIApplication* )application
- {
- #pragma unused(application)
- contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
- summaryLabel.hidden = YES;
- // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
- // method "reachabilityChanged" will be called.
- [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
- //Change the host name here to change the server your monitoring
- remoteHostLabel.text = [NSString stringWithFormat: @"Remote Host: %@", @"www.apple.com"];
- hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
- [hostReach startNotifier];
- [self updateInterfaceWithReachability: hostReach];
- internetReach = [[Reachability reachabilityForInternetConnection] retain];
- [internetReach startNotifier];
- [self updateInterfaceWithReachability: internetReach];
- wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
- [wifiReach startNotifier];
- [self updateInterfaceWithReachability: wifiReach];
- [window makeKeyAndVisible];
- }
- @end
/* File: ReachabilityAppDelegate.m
Abstract: The application's controller. Version: 2.2 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software. In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under
Apple's copyrights in this original Apple software (the "Apple Software"), to
use, reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions
of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may be used
to endorse or promote products derived from the Apple Software without specific
prior written permission from Apple. Except as expressly stated in this notice,
no other rights or licenses, express or implied, are granted by Apple herein,
including but not limited to any patent rights that may be infringed by your
derivative works or by other works in which the Apple Software may be
incorporated. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Copyright (C) 2010 Apple Inc. All Rights Reserved. */ #import "ReachabilityAppDelegate.h"
#import "Reachability.h" @implementation ReachabilityAppDelegate - (void) configureTextField: (UITextField*) textField imageView: (UIImageView*) imageView reachability: (Reachability*) curReach
{
NetworkStatus netStatus = [curReach currentReachabilityStatus];
BOOL connectionRequired= [curReach connectionRequired];
NSString* statusString= @"";
switch (netStatus)
{
case NotReachable:
{
statusString = @"Access Not Available";
imageView.image = [UIImage imageNamed: @"stop-32.png"] ;
//Minor interface detail- connectionRequired may return yes, even when the host is unreachable. We cover that up here...
connectionRequired= NO;
break;
} case ReachableViaWWAN:
{
statusString = @"Reachable WWAN";
imageView.image = [UIImage imageNamed: @"WWAN5.png"];
break;
}
case ReachableViaWiFi:
{
statusString= @"Reachable WiFi";
imageView.image = [UIImage imageNamed: @"Airport.png"];
break;
}
}
if(connectionRequired)
{
statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];
}
textField.text= statusString;
} - (void) updateInterfaceWithReachability: (Reachability*) curReach
{
if(curReach == hostReach)
{
[self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];
NetworkStatus netStatus = [curReach currentReachabilityStatus];
BOOL connectionRequired= [curReach connectionRequired]; summaryLabel.hidden =(netStatus != ReachableViaWWAN);
NSString* baseLabel= @"";
if(connectionRequired)
{
baseLabel= @"Cellular data network is available.\n Internet traffic will be routed through it after a connection is established.";
}
else
{
baseLabel= @"Cellular data network is active.\n Internet traffic will be routed through it.";
}
summaryLabel.text= baseLabel;
}
if(curReach == internetReach)
{
[self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];
}
if(curReach == wifiReach)
{
[self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];
} } //Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateInterfaceWithReachability: curReach];
} - (void) applicationDidFinishLaunching: (UIApplication* )application
{
#pragma unused(application)
contentView.backgroundColor = [UIColor groupTableViewBackgroundColor]; summaryLabel.hidden = YES; // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
// method "reachabilityChanged" will be called.
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil]; //Change the host name here to change the server your monitoring
remoteHostLabel.text = [NSString stringWithFormat: @"Remote Host: %@", @"www.apple.com"];
hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReach startNotifier];
[self updateInterfaceWithReachability: hostReach]; internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
[self updateInterfaceWithReachability: internetReach]; wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
[wifiReach startNotifier];
[self updateInterfaceWithReachability: wifiReach]; [window makeKeyAndVisible]; }
@end
IOS判断网络环境的更多相关文章
- iOS的网络环境判断
网络环境的判断有两种方式 1.Reachability 2.AFNetWorking中 AFNetworkReachabilityManager Reachability步骤 (1)导入Reachab ...
- iOS 判断网络连接状态的几种方法
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #801b80 } p.p2 ...
- iOS开发 利用Reachability判断网络环境
导入头文件:#import "Reachability.h" 然后将 SystemConfiguration.framework 添加进工程: 1.检查当前的网络状态(wifi.W ...
- iOS中4种判断网络请求的方式(系统状态栏、AFNetworking、Reachability、自定义)
iOS 实时判断网络状态 方法一:利用系统状态栏判断网络状态 // 状态栏是由当前app控制的,首先获取当前app UIApplication *app = [UIApplication shared ...
- iOS 判断当前网络状态的三种方法
http://www.cocoachina.com/ios/20171103/21039.html 在项目中,为了好的用户体验,有些场景必须线判断网络状态,然后才能决定改干嘛.比如视频播放,需要线判断 ...
- iOS 检测网络状态 自动判断 认为提示网络改变
检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是让用户了解自己的网络状态,防止一些误会(比如怪应用无能)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验WIFI\3G ...
- 判断浏览器环境(QQ,微信,安卓设备,IOS设备,PC微信环境,移动设备)
判断浏览器环境(QQ,微信,安卓设备,IOS设备,PC微信环境,移动设备) // ===== 判断浏览器环境 ===== // // 判断是否是QQ环境 function isQQ() { retur ...
- iOS开发——Reachability和AFNetworking判断网络连接状态
一.Reachability // 监听网络状态改变的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selec ...
- iOS模拟(糟糕的)网络环境
有时候为了模拟在糟糕的网络环境下app的表现,会故意拔网线(断wifi),苹果其实提供了专门的工具来精确地模拟你在几个预设的场景下的网络连接情况:Network Link Conditioner 点击 ...
随机推荐
- ASP.NET的SEO: 服务器控件背后——SEO友好的Html和JavaScript
本系列目录 假设你需要从一个页面转向其他页面,下面有很多种方式,你是如何选择的呢?你能清晰的说明理由么? <%--链接的表现形式--%> <asp:Hy ...
- linux地址空间划分
LDD讲的很明白了: Linux 是一个虚拟内存系统, 意味着用户程序见到的地址不直接对应于硬件使用的物理地址. 虚拟内存引入了一个间接层, 它允许了许多好事情. 有了虚拟内存, 系统重运行的程序可以 ...
- IIS6 + PHP 访问页面出现:需要进行身份验证的问题
问题描述:之前在IIS6上安装了PHP扩展,发布了一个PHP网站可以正常访问,为了测试网站并发量修改了一个PHP的配置文件以后,再访问就弹出 需要用户名和密码. 同一目录下的 aspx文件可以正常访问 ...
- iOS 层层推进实现代理模式
1.代理模式核心思想:A类委托B类做某件事,然后A类获取B类的执行的返回结果! 举例:女孩想去买电影票,但是自己不亲自去而是委托男孩了解电影电影票信息,同时女孩获得男孩买票的结果,代码模拟实现: /* ...
- 语音分享应用ios源码
该源码是语音分享应用源码,本demo使用了科大讯飞语音识别作为分享内容的输入方式,同时也支持手动键盘输入分享内容,限制分享内容文字不能超过180个字符,分享内容输入完成后可以直接分享,分享SDK使用的 ...
- asp.net webuploader粘贴,拖拽,点击上传图片
demo.html代码:<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset= ...
- javascritp日期函数总结
getDate()与getDay()的区别: obj.getDate()返回一个月中的某一天:obj.getDay()返回一个星期中的某一天. getYear()与getFullYear()的区别: ...
- C#中补0
C#中补0 编写人:CC阿爸 2014-3-16 首先先增加两个左补齐又补齐的函数 #region 该函数动态添加空格,对齐小票 public string AddSpace(string text ...
- Java编程性能优化
1尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面: 第一,控制资源的使用,通过线程同步来控制资 ...
- Spark RDD的依赖解读
在Spark中, RDD是有依赖关系的,这种依赖关系有两种类型 窄依赖(Narrow Dependency) 宽依赖(Wide Dependency) 以下图说明RDD的窄依赖和宽依赖 窄依赖 窄依赖 ...