iOS 判断网络状态 简单示例

添加SystemConfiguration.framework 到工程中

对应的.h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
//如果方法前面加+,就相当于类的静态方法,这里要注意一下
- (BOOL) connectedToNetwork;
@end

对应的.m文件

#import "ViewController.h"
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonHMAC.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netdb.h>
#import <arpa/inet.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [[UIButton alloc]init];
    btn.frame = CGRectMake(20, 20, 40, 40);
    btn.backgroundColor = [UIColor brownColor];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(testOut:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view, typically from a nib.
}
-(BOOL) connectedToNetwork
{
    // Create zero addy
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;

    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);

    if (!didRetrieveFlags)
        {
        printf("Error. Could not recover network reachability flags\n");
        return NO;
        }

    BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
    BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);
    return (isReachable && !needsConnection) ? YES : NO;
}

//在调用的时候,在对应的按钮中加入判断:
- (IBAction)testOut:(UIButton *)sender
{
    if(![self connectedToNetwork])
        {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"网络连接失败,请查看网络是否连接正常!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        }

    NSLog(@"testOut Event");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
时间: 2024-08-03 05:08:11

iOS 判断网络状态 简单示例的相关文章

iOS 判断网络连接状态的几种方法

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #801b80 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px "Comic Sans MS"; color: #801b80 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial; col

iOS开发网络篇—简单介绍ASI框架的使用

iOS开发网络篇—简单介绍ASI框架的使用 说明:本文主要介绍网络编程中常用框架ASI的简单使用. 一.ASI简单介绍 ASI:全称是ASIHTTPRequest,外号“HTTP终结者”,功能十分强大. ASI的实现基于底层的CFNetwork框架,因此运行效率很高.可惜作者早已停止更新,有一些潜在的BUG无人去解决 ASI的github地址 https://github.com/pokeb/asi-http-request ASI的使用参考 http://www.cnblogs.com/dot

Android判断网络状态方法详解

Android 判断网络状态这一应用技巧在实际应中是比较重要的.那么,在Android操作系统中,如何能够正确的判断我们所连接的网络是否断开恩?今天我们就针对这一应用技巧进行一个详细的分析. 1.public class ConnectionChangeReceiverextends BroadcastReceiver 2.{ [email protected] 4.public void onReceive( Context context,Intent intent ) 5.{ 6.Conn

IOS判断网络环境

https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html 我下载的是vertion2.2 开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息.如果没有处理它们,是不会通过Apple的审查的. Reachability Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法. 在你的程序中使用 1.Reachability 只须将该例程中的

android——判断网络状态

平常我们玩qq的时候我们没联网 ,qq上面回显示"网络连接不给力,请检查网络连接"那么它是 通过什么方式判断网络是否连接的呢? 下面将写个案例展示如何检查网络连接状态的 由于我们这里只是测试下所以我们的项目只是简单的实现点击一个按钮判断设备网络是否连接如果 网络已连接则弹出一个消息框提示"网络通畅"反之则提示"您的网络真的连接 了吗" 1.新建一个android项目textNet(设备版本为4.4.2) 2.打开activity_main.xml

Android Network -------------------判断网络状态(网络的连接,改变,和判断2G/3G/4G)

现在app大多都需要从网络上获得数据.所以访问网络是在所难免.但是再访问网络之前,我们应该先做一下网络的状态判断.其实在访问网络之前我们要做一些状态判断,对应一些状态判断来做处理,并不是直接使用Http访问网络即可.很多人在开发就经常把网络这块直接跳过,直接访问网络,可以一断网,各种体验效果不好,不是说app没法用,只是体验效果差.还有,就是我们可能为用户考虑,因为现在一般连网是wifi和手机流量,都知道后者收费是比较高的.假如我们的app加载的图片或者有大的数据下载操作,可是用户的本意是要是在

ios 判断网络

网络的重要性,相信大家都知道了.这次介绍下ios是如何判断网络的好坏的,由于在开发中用到,所以分享给大家,很简单.这里要用到Reachability封装类来实现,大家可以网上去下载Reachability.m 和Reachability.h文件,需要我提供请留言. 具体代码: 在AppDelegate里面实现: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary

iOS 检测网络状态

1.为什么要检测网络状态? 1.1 让用户知道自己的网络状态,防止用户埋怨"这个应用太垃圾,获取数据那么慢" 1.2 根据用户的网络状态,智能处理,提升用户体验 例如某些手机浏览器,检测到用户网络是2G/3G时,会自动切换为无图模式 2.手动触发 2.1 首先下载苹果的示例程序Reachability, 取得示例程序里的Reachability.h和Reachability.m, 添加到自己项目里 代码如下 - (void)touchesBegan:(NSSet *)touches w

iOS 判断网络是否连接

1 //判断网络是否连接 2 + (BOOL) connectedToNetwork 3 { 4 //创建零地址,0.0.0.0的地址表示查询本机的网络连接状态 5 struct sockaddr_in zeroAddress; //struct用来向方法中传递复杂的参数(把参数当作对象,这样便于扩展) 6 bzero(&zeroAddress, sizeof(zeroAddress)); 7 zeroAddress.sin_len = sizeof(zeroAddress); 8 zeroAd