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(@"网络请求完成");
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)