网络请求的封装

本文详细介绍了iOS开发中使用NSURLConnection进行网络请求的方法,包括同步和异步请求的实现方式,并展示了如何处理请求过程中的各种事件,如接收响应、接收数据、完成加载及失败情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDelegate,NSURLConnectionDataDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //同步请求
    //[NSURLConnection sendSynchronousRequest:<#(NSURLRequest *)#> returningResponse:(NSURLResponse *__autoreleasing *) error:<#(NSError *__autoreleasing *)#>];
    
    /*
     NSURL对象初始化注意:
     1.url里面有空格。
     2.url里面不能有汉字。
     */
    
    NSString *urlString = @"http://d3.s.hjfile.cn/2012/201202_3/43904b09-24e1-4fdb-8b46-d3dba3323278.mp3";
    //有中文需要utf8编码
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    //1.url
    NSURL *url = [NSURL URLWithString:urlString];
    //如果url里面有汉字,需要编码
    
    //取以/分割,分割的字符串的最后一部分
    //[url lastPathComponent];
    
    
    NSLog(@"--- %@",url);
    
    
    //2.请求对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    
#pragma mark - 异步请求的第1种方式:
    /*
     sendAsynchronousRequest:请求对象
     queue:队列
     completionHandler:请求完成的回调
     */
    //3.发送异步请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSLog(@"请求到的数据----%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }];
    
    //[NSURLConnection connectionWithRequest:<#(NSURLRequest *)#> delegate:<#(id)#>];
    
#pragma mark - 异步请求的第2种方式:
    //1.创建NSURLConnection对象
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
    //2.手动启动请求
    [connection start];
    
    
#pragma mark - 异步请求的第3种方式:
    //发送异步请求
    [NSURLConnection connectionWithRequest:request delegate:self];
    
  
}

#pragma mark - NSURLConnection
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //1.文件名字
    NSString *fileName = [response suggestedFilename];
    NSLog(@"filename=%@",fileName);
    
    //2.文件大小,单位字节
    long long fileSize = [response expectedContentLength];
    
    
    //3.文件类型
    NSString *type = [response MIMEType];
    NSLog(@"type=%@",type);
    
    
    //4.状态码
    //404,403 ,500,200
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSInteger code = httpResponse.statusCode;
    NSLog(@"状态码:%ld",code);
    
    
    //5.响应头信息
    NSDictionary *headerFields = [httpResponse allHeaderFields];
    NSLog(@"响应头:%@",headerFields);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //1.追加数据
    //2.计算进度
    //3.刷新界面
    
    //4.写入数据
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //1.刷新界面
    //2.数据解析和封装数据模型
    
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    
}



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

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值