`
啸笑天
  • 浏览: 3438089 次
  • 性别: Icon_minigender_1
  • 来自: China
社区版块
存档分类
最新评论

判断网络环境

 
阅读更多

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

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Phonegap判断网络类型及状态

    cordova 3.4 版本的API 判断网络的连接状态 调用phonegap API 判断网络类型 :2G 3G 4G wifi 等等 博客: http://blog.csdn.net/aaawqqq/article/details/22220319

    iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi

    主要给大家介绍了关于iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

    使用JS在浏览器中判断当前网络连接状态的几种方法

    使用JS在浏览器中判断当前网络状态的几种方法如下: 1. navigator.onLine 2. ajax请求 3. 获取网络资源 4. bind() 1. navigator.onLine 通过navigator.onLine判断当前网络状态: if(navigator.onLine){ ... }...

    判断网络是否WIFI,4G, 4G+

    提供判断网络是否可用,当前是否是wifi,4g或者4g+,如果不可用,并给出提示,调转到系统网络设置界面

    安卓实现网络情况监测,网络连接是否有效,判断移动网络还是WIFI

    安卓端实现对APP实时网络的检测,可判断此时手机能否访问网络,以及判断出此时是在使用移动流量还是WIFI,代码里有解释和介绍。博客介绍可看https://blog.csdn.net/weixin_38611617/article/details/115296055

    RdViewer Pro V4.7.0 自建服务器版

    7、自动判断网络环境,实现UDP P2P网络穿透和内网直连。 8、人性化的远程文件管理功能和细节处理(网络状态检测、屏幕截图Banner等),带给你不一样的视觉和操作体验。 支持系统和环境: WinXp-Win11所有操作系统,...

    CrossNet-Beta:红队行动中利用白利用,免杀,自动判断网络环境生成钓鱼重置文件

    生成马需要visual studio环境,这里是visual studio 2017 已实现 判断出网方式TCP,DNS√ 依据不同的出网方式进行shellcode加载√ 利用lolbins结合dll劫持执行shellcode√ Beta 1.0.2 修改静态内存申请后添加临时...

    android自动更新

    九、library支持判断网络环境,移动数据下智能提示,防止流量外漏。 十、考虑到减少与后台人员的配合的高度耦合,增加了自定义model的入口,具体使用请关注使用方式。 十一、提供两种模式的版本更新,一种是对话框...

    ICND课件-4-管理网络环境

    ——判断网络故障的原因并解决之 2、 主要内容、知识点  网络基础知识  操作和配置Cisco IOS设备  管理网络环境  基于TCP的互联网络(其中包含VLSM)  IP路由  ACL访问控制列表及NAT  广域网

    pb 用Ping方法 判断网络是否连通 IP地址是否有效 powerbuild

    pb 用Ping方法 判断网络是否连通 IP地址是否有效 pb 用Ping方法 判断网络是否连通 IP地址是否有效

    在ios工程中如何判断当前网络环境是IPV6还是IPV4 源码下载

    判断手机连的wifi是ipv4的地址还是ipv6的地址

    Reachability 网络判断库文件

    Reachability 判断当前应用网络是否连接 以及当前网络处于什么网络环境(2G 或 WIFI)

    qt获得网络状态testNetOnIine

    5.保证无毒 1.简单,方便,实用 3.实例可以自行改用 1.如有非法,本人无法律责任! 8.更多作品,查找标签“朱建强”7.... 4.如需联系我请看左边数字!1.如不知代表何物,那就放弃计算机吧! 0....CSDN老板不让我上传联系方式。

    使用PHP判断是否连接上网络

    使用PHP判断是否连接上网络 一、 开发环境 1、环境搭建:Windows 7+Apache 2.4.18+MySQL 5.7.11+PHP 7.1.0。 2、文本编辑器:Sublime 3。 二、主要技术 本案例主要使用PHP通过判断能否打开网页从而来判断能否连接上...

    Android开发准确获取手机IP地址的两种方式

    第一步:首先是判断网络环境: String ip; ConnectivityManager conMann = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mobileNetworkInfo = conMann.get

    工作与家庭网络切换.bat

    此代码可以自动判断网卡,并选择描述字符包含“Network” 的网卡进行网络配置,可自行设定工作网络环境的网络参数,程序会自动判断是否为工作网络,若是工作网络则切换为动态IP的家庭网络,否则转换为静态IP的工作...

    ios-LHHTTPRequest.zip

    LHHTTPRequest 是基于CFNetWork的... 2.LHNetWorkReachability 此类是判断网络环境的类,可以获取当前网络环境,监测网络变化(通知方式,block方式) 使用前 需导入CFNetwork.framework SystemConfiguration.framework

    网络环境下探究教学评价分析

    网络环境下探究教学评价分析,董双威,解月光,基于网络的探究教学对实现信息技术教学应用的价值目标发挥着重要的作用,它在评价的功能目标、价值判断、对象内容、方法工具等方

    delphi 快速判断sql服务器的服务能否连接上

    在做数据库开发的时候经常会遇到数据库连不上的情况,这个时候程序就会卡死,要等好长时间才能有反应,为此就做了一个快速数据库边接的程序,仅供大家参考。...本程序在delphi 2007环境下开发,在xp环境下开发成功

Global site tag (gtag.js) - Google Analytics