1、网络监测
Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
self.reach = reach;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged) name:kReachabilityChangedNotification object:nil];
[reach startNotifier];
NotReachable = 0,
ReachableViaWiFi = 2,
ReachableViaWWAN = 1
- (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、建立连接
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable 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:^{
}];
}];
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";
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(@"网络请求完成");
}
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 想让你多爱自己一些的开源计时器
· Cursor预测程序员行业倒计时:CTO应做好50%裁员计划
· 大模型 Token 究竟是啥:图解大模型Token
· 用99元买的服务器搭一套CI/CD系统
· 如何在 .NET 中 使用 ANTLR4