iOS中的网络请求 和 网络监测
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中的网络请求 和 网络监测的更多相关文章
- iOS中发送HTTP请求的方案
在iOS中,常见的发送HTTP请求的方案有 苹果原生(自带) NSURLConnection:用法简单,最古老最经典的一种方案 NSURLSession:功能比NSURLConnection更加强大, ...
- 网络请求以及网络请求下载图片的工具类 android开发java工具类
package cc.jiusan.www.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; ...
- ios中判断当前手机的网络状态
typedef enum { NETWORK_TYPE_NONE= 0, NETWORK_TYPE_2G= 1, NETWORK_TYPE_3G= 2, NETWORK_TYP ...
- iOS中POST异步请求
POST异步请求(代理) 1.遵循<NSURLConnectionDataDelegate> @interface ViewController ()<NSURLConnection ...
- Android网络请求通信之Volley
一.Volley简介 Volley网络框架是Google公司在2013年发布的一款Android平台上的网络请求通信库.以下是对Volley的简单归纳. Volley的优点: 使网络通信更快.更简单. ...
- Android 网络请求详解
我们知道大多数的 Android 应用程序都是通过和服务器进行交互来获取数据的.如果使用 HTTP 协议来发送和接收网络数据,就免不了使用 HttpURLConnection 和 HttpClient ...
- App 组件化/模块化之路——如何封装网络请求框架
App 组件化/模块化之路——如何封装网络请求框架 在 App 开发中网络请求是每个开发者必备的开发库,也出现了许多优秀开源的网络请求库.例如 okhttp retrofit android-asyn ...
- CHNetRequest网络请求
Paste JSON as Code • quicktype 软件的使用 iOS开发:官方自带的JSON使用 JSON 数据解析 XML 数据解析 Plist 数据解析 NetRequest 网络数据 ...
- charles重发网络请求&模拟慢速网络&过滤网络请求
重发网络请求&模拟慢速网络&过滤网络请求 重发网络请求:后端调试的过程中,一直在客户端进行点点点比较麻烦,此时直接发送请求比较方便查看调试后的结果 模拟慢速网络:用户的网络不能一直是快 ...
随机推荐
- Windows 7 不同安装模式简要区别(图解)
★ 你可能对GHOST不支持AHCI感到迷惑,实际上,写过GHOST一键安装批处理的都知道一个叫FINDCD.EXE的小程序,可是这个程序老 了,AHCI模式光驱他找不到了,找不到光驱动意味着光盘中G ...
- 解决UITableView头部空白
解决方式1: self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.ta ...
- 使用maphilight高亮显示map的指定area
引用jquery.maphilight.js之后,调用如下方法: //obj参数是代表一个或者多个area的jQuery对象 function areaLight(obj) { var data = ...
- MySQL主从复制的原理及配置
[http://www.jb51.net/article/50053.htm] MySQL 数据库的高可用性架构: 集群,读写分离,主备.而后面两种都是通过复制来实现的.下面将简单 ...
- Visual Studio无法添加断点
今天在写代码的时候突然发现无法添加断点,更加详细的场景是“按F9可以添加调试行,但是断点不显示,且显示代码行数左边的灰色区域不见了”找了各种方法也没有解决,然后重启.修复甚至重装都不行,最后在万千网页 ...
- DeleteDC() 与 ReleaseDC() 的区别 [转]
DeleteDC 该函数删除指定的设备上下文环境(DC). 原型: BOOL DeleteDC(HDC hdc): 参数: hdc:设备上下文环境的句柄. 返回值: 成功,返回非零值:失败,返回零.调 ...
- extjs tablepanel 高度自适应有关问题
extjs tablepanel 高度自适应问题 项目中为了给客户好点的功能切换体验,想到了用extjs的tabpanel 在页面中用了tabpanel后,高度新打开的tab页的iframe 的高度总 ...
- 【转】REST on Rails指南
REST on Rails指南1:理解资源 这是来自http://www.softiesonrails.com的REST简明指南. PART I 在理解REST on Rails之前,有必要先思考一下 ...
- iOS 7 Pushing the Limits - Good & Bad Namings in Cocoa
Cocoa is a dynamically typed language, and you can easily get confused about what type you are worki ...
- 【React Native 实战】商品分类
1.前言 商品分类是各种app常见的一种操作,一般都是左右两栏构成,左边栏是商品的分类,右边栏是商品的展示,同时左右两栏都可以滑动.今天我们就用React Native来实现这种效果. 实现内容:1) ...