1、网络监测


//根据主机名判断网络是否连接 Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"]; self.reach = reach; //注册网络监听通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged) name:kReachabilityChangedNotification object:nil]; //开启监听 [reach startNotifier]; //根据currentReachabilityStatus判断网络状态 NotReachable = 0, //没有网络
ReachableViaWiFi = 2, //不用花钱WIFI
ReachableViaWWAN = 1 //2G,3G,4G
- (void)dealloc { //把当前的对象所有通知删除
[[NSNotificationCenter defaultCenter] removeObserver:self]; //停止监听
[self.reach stopNotifier];
}

2、异步网络请求

//1、创建URL,访问网络资源的唯一地址

NSURL * url = [NSURL URLWithString:@"http://192.168.1.123/demo.json"];
//2、创建网络请求

/* cachePolicy 缓存策略 NSURLRequestUseProtocolCachePolicy = 0, //自动缓存策略 NSURLRequestReloadIgnoringLocalCacheData = 1, //每次都请求网络,无论本地是否存在缓存 NSURLRequestReturnCacheDataElseLoad = 2,//如果有缓存返回缓存,没有就加载网络 NSURLRequestReturnCacheDataDontLoad = 3,//如果有缓存返回缓存,没有也不加载网络 timeoutInterval 请求超时 默认超时时间是60 一般设置 10 - 20s */ NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:15];
//3、建立连接
//sendAsynchronousRequest 建立异步网络连接

//queue  可以传主队列,或全局队列

//[NSOperationQueue mainQueue]   不用调到主队列直接更新

//[[NSOperationQueue alloc] init]  则需要调回主线程更新UI

[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

    //response 服务器响应信息,一般下载时有用

    //data  服务器返回的数据

    //connectionError 网络请求错误

    //服务器与客户端是以二进制流通讯的

    NSLog(@"%@",data);

    [data writeToFile:@"/Users/dahuan/Desktop/test" atomically:YES];

    NSLog(@"%@",[NSThread currentThread]);

    if (connectionError) {

        NSLog(@"错误信息:%@",connectionError);

    } else {

        NSLog(@"响应信息:%@",response);

        NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"%@",string);

        [self.webView loadHTMLString:string baseURL:nil];

    }

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

        //更新UI

    }];

}];

3、同步网络请求

    NSURL * url = [NSURL URLWithString:@"http://192.168.1.123/demo.json"];

    NSURLRequest * request = [NSURLRequest requestWithURL:url];

    //同步网络请求

    NSURLResponse * response = nil;
NSError * error = nil; NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",string);
NSLog(@"%@",response);

4、NSURLConnection代理方法

    self.data = [NSMutableData data];

    NSString * urlString = @"http://192.168.1.123/把悲伤留给自己.mp3";

    //NSString 中包含中文字符时转换为NSURL
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL * url = [NSURL URLWithString:urlString]; NSURLRequest * request = [NSURLRequest requestWithURL:url]; NSURLConnection * connect = [NSURLConnection connectionWithRequest:request delegate:self]; //开启网络连接
[connect start];
//服务器返回响应信息1
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSLog(@"%@",response);

}
//接受数据(多次调用)2
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [self.data appendData:data];

    NSLog(@"%@",data);

}
//请求完成3
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    [self.data writeToFile:@"/Users/dahuan/Desktop/aaaaa.mp3" atomically:YES];
NSLog(@"网络请求完成"); }

iOS中的网络请求 和 网络监测的更多相关文章

  1. iOS中发送HTTP请求的方案

    在iOS中,常见的发送HTTP请求的方案有 苹果原生(自带) NSURLConnection:用法简单,最古老最经典的一种方案 NSURLSession:功能比NSURLConnection更加强大, ...

  2. 网络请求以及网络请求下载图片的工具类 android开发java工具类

    package cc.jiusan.www.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; ...

  3. ios中判断当前手机的网络状态

    typedef enum {    NETWORK_TYPE_NONE= 0,    NETWORK_TYPE_2G= 1,    NETWORK_TYPE_3G= 2,    NETWORK_TYP ...

  4. iOS中POST异步请求

    POST异步请求(代理) 1.遵循<NSURLConnectionDataDelegate> @interface ViewController ()<NSURLConnection ...

  5. Android网络请求通信之Volley

    一.Volley简介 Volley网络框架是Google公司在2013年发布的一款Android平台上的网络请求通信库.以下是对Volley的简单归纳. Volley的优点: 使网络通信更快.更简单. ...

  6. Android 网络请求详解

    我们知道大多数的 Android 应用程序都是通过和服务器进行交互来获取数据的.如果使用 HTTP 协议来发送和接收网络数据,就免不了使用 HttpURLConnection 和 HttpClient ...

  7. App 组件化/模块化之路——如何封装网络请求框架

    App 组件化/模块化之路——如何封装网络请求框架 在 App 开发中网络请求是每个开发者必备的开发库,也出现了许多优秀开源的网络请求库.例如 okhttp retrofit android-asyn ...

  8. CHNetRequest网络请求

    Paste JSON as Code • quicktype 软件的使用 iOS开发:官方自带的JSON使用 JSON 数据解析 XML 数据解析 Plist 数据解析 NetRequest 网络数据 ...

  9. charles重发网络请求&模拟慢速网络&过滤网络请求

    重发网络请求&模拟慢速网络&过滤网络请求 重发网络请求:后端调试的过程中,一直在客户端进行点点点比较麻烦,此时直接发送请求比较方便查看调试后的结果 模拟慢速网络:用户的网络不能一直是快 ...

随机推荐

  1. mahout算法源码分析之Itembased Collaborative Filtering(四)共生矩阵乘法

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 经过了SimilarityJob的计算共生矩阵后,就可以开始下面一个过程了,这个过程主要是共生矩阵的乘法 ...

  2. 用JS或jQuery访问页面内的iframe,兼容IE/FF

    用JS或jQuery访问页面内的iframe,兼容IE/FF js或者jQuery访问页面中的框架也就是iframe.注意:框架内的页面是不能跨域的! 假设有两个页面,在相同域下. index.htm ...

  3. iOS上传文件,有关http上传协议-RFC1867

    以上是抓包HTML input file标签上传的内容 只要模拟上面http 的header跟body就可以成功. 整体说明: post 上传文件时,以在http body里面带参数,参数的格式,根据 ...

  4. 获取oracle 表字段,表名,以及主键之类等等的信息

    数据库版本号:select * from v$version 数据库名:select * from v$instance 注意: 我在C#项目中查询语句的时候报“ORA-00911: 无效字符” 的错 ...

  5. How to get cocoapods work on Yosemite

    查看原文:http://leancodingnow.com/how-to-get-cocoapods-work-on-yosemite/ 今天升级了Mac OS X 10.10-Yosemite以后运 ...

  6. 对Slony-I中wait on的理解

    http://slony.info/documentation/2.1/advanced.html#AEN1425 4.1.2. Event Confirmations When an event i ...

  7. uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...

  8. ubuntu12.04_64bit adb shell

    1.#adb shell 提示error: insufficient permissions for device 解决办法: 1)sudo gedit /etc/udev/rules.d/51-an ...

  9. Android调用相机并将照片存储到sd卡上

    Android中实现拍照有两种方法,一种是调用系统自带的相机,然后使用其返回的照片数据. 还有一种是自己用Camera类和其他相关类实现相机功能,这种方法定制度比较高,洗染也比较复杂,一般平常的应用只 ...

  10. 读loadBalance技术的一些笔记

    以前知道loadbalance的原理,但是仅仅是浅浅的了解过,今天看了一篇 10多年前 一位大神级别人物 写的文章 顿时学习了 http://www.linuxvirtualserver.org/zh ...