ios开发进阶之网络05 NSURLConnection AFN

一 NSURLConnection和NSRunLoop

  • 利用NSURLConnection发送一个请求, 系统会就自动将NSURLConnection加入到当前线程的RunLoop中
    • 如果是在主线程中发送请求, 主线程的RunLoop永远存在, 所以NSURLConnection不会被释放
    • 如果是在子线程中发送请求, 子线程默认没有RunLoop, 所以NSURLConnection会被释放
  • 默认情况会将NSURLConnection添加当前线程到RunLoop,如果是在子线程中调用NSURLConnection可能会有问题, 因为子线程默认没有RunLoop

  • 如何让代理方法在子线程中执行?

    • [conn setDelegateQueue:[[NSOperationQueue alloc] init]];
  • 注意:

    • NSURLConnection是异步请求
        // 1.发送请求
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_01.png"]] ;
       NSRunLoop *runloop = [NSRunLoop currentRunloop];
       NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
       // 设置回调代理方法在哪个线程中执行
        [conn setDelegateQueue:[[NSOperationQueue alloc] init]];
        // 2.启动runLoop
        // 默认情况下子线程没有RunLoop, 所以需要启动
        [runloop run];

        // 3.如果手动发送请求, 系统内部会自动帮子线程创建一个RunLoop
    //    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    //    [conn start];

二 NSURLSession基本使用

  • Task类型

这里写图片描述

  • 使用步骤

    • 创建NSURLSession
    • 创建Task
    • 执行Task
  • 方法一 代码

NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [@"username=520it&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];

    // 1.创建NSURLSession
    NSURLSession *session = [NSURLSession sharedSession];
    // 2.利用NSURLSession创建Task
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
         NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }];
    // 3.执行Task
    [task resume];
  • 方法二 省略request
    • 应用场景:不需要设置请求头信息或者发送POST请求
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];

    // 1.创建NSURLSession
    NSURLSession *session = [NSURLSession sharedSession];
    // 2.利用NSURLSession创建Task
    // 如果是通过传入url的方法创建Task, 方法内部会自动根据URL创建一个Request
    // 如果是发送Get请求, 或者不需要设置请求头信息, 那么建议使用当前方法发送请求
    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }];
    // 3.执行Task
    [task resume];

三 NSURLSession-downloadTask

  • DownloadTask默认已经实现边下载边写入
    // 1.创建request
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 2.创建session
    NSURLSession *session = [NSURLSession sharedSession];

    // 3.创建下载任务
    NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
        NSLog(@"%@", location.absoluteString);

        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *toPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        toPath = [toPath stringByAppendingPathComponent:response.suggestedFilename];
        // 将下载好的文件从location移动到cache
        [manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:toPath] error:nil];
    }];

    // 4.开启下载任务
    [task resume];

四 监听下载进度

 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_02.mp4"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 1.创建Session
    /*
     第一个参数:Session的配置信息
     第二个参数: 代理
     第三个参数: 决定了代理方法在哪个线程中执行
     */
    self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];

    // 2.根据Session创建Task
    self.task = [self.session downloadTaskWithRequest:request];

    // 3.执行Task
    [self.task resume];
  • DownloadTask监听下载进度
/**
 * 每当写入数据到临时文件时,就会调用一次这个方法
 * totalBytesExpectedToWrite:总大小
 * totalBytesWritten: 已经写入的大小
 * bytesWritten: 这次写入多少
 */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
    NSLog(@"正在下载: %.2f", 1.0 * totalBytesWritten / totalBytesExpectedToWrite);
}

/*
 * 根据resumeData恢复任务时调用
 * expectedTotalBytes:总大小
 * fileOffset: 已经写入的大小
 */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
    NSLog(@"didResumeAtOffset fileOffset = %lld , expectedTotalBytes = %lld", fileOffset, expectedTotalBytes);
}
/**
 * 下载完毕就会调用一次这个方法
 */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
    NSLog(@"下载完毕");
    // 文件将来存放的真实路径
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:downloadTask.response.suggestedFilename];

    // 剪切location的临时文件到真实路径
    NSFileManager *mgr = [NSFileManager defaultManager];
    [mgr moveItemAtURL:location toURL:[NSURL fileURLWithPath:file] error:nil];
}
/**
 * 任务完成
 */
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    NSLog(@"didCompleteWithError");
}
  • DownloadTask断点下载
    • 暂停: suspend
    • 继续: resume
    • 取消: cancel
      • 任务一旦被取消就无法恢复
    • 区别并返回下载信息
    //取消并获取当前下载信息
[self.task cancelByProducingResumeData:^(NSData *resumeData) {
        self.resumeData = resumeData;
    }];

    // 根据用户上次的下载信息重新创建任务
    self.task = [self.session downloadTaskWithResumeData:self.resumeData];
    [self.task resume];

五 离线断点下载

  • 使用NSURLSessionDataTask
NSMutableURLRequest *reuqest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"]];
// 核心方法, 实现从指定位置开始下载
NSInteger currentBytes = KCurrentBytes;
NSString *range = [NSString stringWithFormat:@"bytes=%zd-", currentBytes];
[reuqest setValue:range forHTTPHeaderField:@"Range"];

// 核心方法, 自己实现边下载边写入
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data;
  • 优化代码实例:
@interface ViewController ()<NSURLSessionDataDelegate>

@property (weak, nonatomic) IBOutlet UIProgressView *progressView;

@property (nonatomic, assign)NSUInteger totalLength; /**< 总大小 */
@property (nonatomic, assign)NSUInteger currentLength; /**< 当前已经下载的大小 */

@property (nonatomic, strong) NSOutputStream *outputStream ; /**< 输出流 */

@property (nonatomic, strong) NSURLSession *session; /**< session */

@property (nonatomic, strong) NSURLSessionDataTask *task; /**< 任务 */

@property (nonatomic, copy) NSString *path; /**< 文件路径 */

@end
#pragma mark - lazy
- (NSURLSession *)session
{
    if (!_session) {

         // 1.创建Session
        _session =  [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    }
    return _session;
}

- (NSURLSessionDataTask *)task
{
    if (!_task) {
        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_02.mp4"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        // 设置请求头
        NSString *range = [NSString stringWithFormat:@"bytes:%zd-", [self getFileSizeWithPath:self.path]];
        [request setValue:range forHTTPHeaderField:@"Range"];

        _task = [self.session dataTaskWithRequest:request];
    }
    return _task;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 初始化操作
    // 1.初始化文件路径
    self.path = [@"minion_02.mp4" cacheDir];
    // 2.初始化当前下载进度
    self.currentLength = [self getFileSizeWithPath:self.path];
}

- (IBAction)start:(UIButton *)sender
{
    // 3.执行Task
    [self.task resume];
}
- (IBAction)pause:(UIButton *)sender
{
    [self.task suspend];
}
- (IBAction)goOn:(UIButton *)sender
{
    [self.task resume];
}
// 从本地文件中获取已下载文件的大小
- (NSUInteger)getFileSizeWithPath:(NSString *)path
{
    NSUInteger currentSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil][NSFileSize] integerValue];
    return currentSize;
}
#pragma mark - NSURLSessionDataDelegate
// 接收到服务器的响应时调用
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
    NSLog(@"didReceiveResponse");
    // 告诉系统需要接收数据
    completionHandler(NSURLSessionResponseAllow);

    // 初始化文件总大小
    self.totalLength = response.expectedContentLength + [self getFileSizeWithPath:self.path];

    // 打开输出流
    self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.path append:YES];
    [self.outputStream open];
}

// 接收到服务器返回的数据时调用
// data 此次接收到的数据
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    // 累加已经下载的大小
    self.currentLength += data.length;

    // 计算进度
    self.progressView.progress = 1.0 * self.currentLength / self.totalLength;

    // 写入数据
    [self.outputStream write:data.bytes maxLength:data.length];
}

// 请求完毕时调用, 如果error有值, 代表请求错误
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    NSLog(@"didCompleteWithError");

    // 关闭输出流
    [self.outputStream close];
}

六 uploadTask基本使用

  • 设置设置请求头, 告诉服务器是文件上传
    • multipart/form-data; boundary=lnj
  • 必须自己严格按照格式拼接请求体
  • 请求体不能设置给request, 只能设置给fromData
    • The body stream and body data in this request object are ignored.
    // 如果设置在request中会被忽略
//    request.HTTPBody = body;
[[self.session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
    }] resume];
  • 注意:以下方法用于PUT请求
    • fromFile方法是用于PUT请求上传文件的,而服务器只支持POST请求上传文件
[self.session uploadTaskWithRequest:<#(nonnull NSURLRequest *)#> fromFile:<#(nonnull NSURL *)#>]

七 监听上传进度

/*
 bytesSent: 当前发送的文件大小
 totalBytesSent: 已经发送的文件总大小
 totalBytesExpectedToSend: 需要发送的文件大小
 */
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{
    NSLog(@"%zd, %zd, %zd", bytesSent, totalBytesSent, totalBytesExpectedToSend);
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    NSLog(@"%s", __func__);
}

八 NSURLSessionConfiguration

  • NSURLSessionConfiguration对象用于初始化NSURLSession对象。
  • 构造方法:NSURLSessionConfiguration有三个类构造函数
    • defaultSessionConfiguration返回标准配置
      • 与NSURLConnection的网络协议栈是一样的,具有相同的共享NSHTTPCookieStorage,共享NSURLCache和共享NSURLCredentialStorage。
    • ephemeralSessionConfiguration返回一个预设配置
      • 没有持久性存储的缓存,Cookie或证书。对于实现像”秘密浏览”功能的功能来说,是很理想的。
    • backgroundSessionConfiguration创建一个后台会话
      • 后台会话不同于常规的,普通的会话,它可以在应用程序挂起,退出,崩溃的情况下运行上传和下载任务。初始化时指定的标识符,被用于向任何可能在进程外恢复后台传输的守护进程提供上下文。
  • NSURLSessionConfiguration的属性

    • allowsCellularAccess(允许蜂窝访问)和discretionary(自行决定)被用于节省通过蜂窝连接的带宽。

      • 建议在使用后台传输的时候,使用discretionary属性,而不是allowsCellularAccess属性,因为它会把WiFi和电源可用性考虑在内
    • timeoutIntervalForRequest和timeoutIntervalForResource指定了请求以及该资源的超时时间间隔。

      • 许多开发人员试图使用timeoutInterval去限制发送请求的总时间,但这误会了timeoutInterval的意思:报文之间的时间。timeoutIntervalForResource实际上提供了整体超时的特性,这应该只用于后台传输,而不是用户实际上可能想要等待的任何东西。

九 AFN

  • 什么是AFN
    • 全称是AFNetworking,是对NSURLConnection的一层封装

AFN结构体

  • NSURLConnection

    • AFURLConnectionOperation
    • AFHTTPRequestOperation
    • AFHTTPRequestOperationManager(封装了常用的 HTTP 方法)
      • 属性
        • baseURL :AFN建议开发者针对 AFHTTPRequestOperationManager 自定义个一个单例子类,设置 baseURL, 所有的网络访问,都只使用相对路径即可
        • requestSerializer :请求数据格式/默认是二进制的 HTTP
        • responseSerializer :响应的数据格式/默认是 JSON 格式
        • operationQueue
        • reachabilityManager :网络连接管理器
          • 方法
          • manager :方便创建管理器的类方法
          • HTTPRequestOperationWithRequest :在访问服务器时,如果要告诉服务器一些附加信息,都需要在 Request 中设置
          • GET
          • POST
  • NSURLSession

    • AFURLSessionManager
    • AFHTTPSessionManager(封装了常用的 HTTP 方法)

      • GET
      • POST
      • UIKit + AFNetworking 分类
      • NSProgress :利用KVO
    • 半自动的序列化&反序列化的功能

      • AFURLRequestSerialization :请求的数据格式/默认是二进制的
      • AFURLResponseSerialization :响应的数据格式/默认是JSON格式
    • 附加功能

      • 安全策略
        • HTTPS
        • AFSecurityPolicy
      • 网络检测
        • 对苹果的网络连接检测做了一个封装
        • AFNetworkReachabilityManager

十 AFN基本使用

  • NSURLConnection包装方法

    • GET
    // 1.创建AFN管理者
    // AFHTTPRequestOperationManager内部包装了NSURLConnection
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // 2.利用AFN管理者发送请求
    NSDictionary *params = @{
                             @"username" : @"520it",
                             @"pwd" : @"520it"
                             };
    [manager GET:@"http://120.25.226.186:32812/login" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSLog(@"请求成功---%@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"请求失败---%@", error);
    }];
    • POST
    // 1.创建AFN管理者
    // AFHTTPRequestOperationManager内部包装了NSURLConnection
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // 2.利用AFN管理者发送请求
    NSDictionary *params = @{
                             @"username" : @"520it",
                             @"pwd" : @"520it"
                             };
    [manager POST:@"http://120.25.226.186:32812/login" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"请求成功---%@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败---%@", error);
    }];
  • NSURLSession包装方法

    • GET
    // 1.创建AFN管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.利用AFN管理者发送请求
    NSDictionary *params = @{
                             @"username" : @"520it",
                             @"pwd" : @"520it"
                             };
    
    [manager GET:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
         NSLog(@"请求成功---%@", responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"请求失败---%@", error);
    }];
    • POST
    // 1.创建AFN管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.利用AFN管理者发送请求
    NSDictionary *params = @{
                             @"username" : @"520it",
                             @"pwd" : @"520it"
                             };
    
    [manager POST:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"请求成功---%@", responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"请求失败---%@", error);
    }];
  • 文件下载
    // 1.创建AFN管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    // 2.利用AFN管理者发送请求
     NSURLRequest *reuqest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_02.mp4"]];

    [[manager downloadTaskWithRequest:reuqest progress:nil  destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        // targetPath: 已经下载好的文件路径
        NSLog(@"targetPath = %@", targetPath);
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[path stringByAppendingPathComponent:response.suggestedFilename]];
        // 返回需要保存文件的目标路径
        return documentsDirectoryPath;
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        NSLog(@"filePath = %@", filePath);
    }] resume];
  • 监听进度
/*
     要跟踪进度,需要使用 NSProgress,是在 iOS 7.0 推出的,专门用来跟踪进度的类!
     NSProgress只是一个对象!如何跟踪进度!-> KVO 对属性变化的监听!
     @property int64_t totalUnitCount;        总单位数
     @property int64_t completedUnitCount;    完成单位数
     */
    NSProgress *progress = nil;
    // 注册通知
    [progress addObserver:self forKeyPath:@"completedUnitCount" options:NSKeyValueObservingOptionNew context:nil];
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"%@", object);
    /**
     准确的获得进度
     localizedDescription               10%
     localizedAdditionalDescription     completed 32,768 of 318,829
     fractionCompleted                  0.102776(completedUnitCount/totalUnitCount)
     */
    if ([object isKindOfClass:[NSProgress class]]) {
        NSProgress *p = (NSProgress *)object;
        NSLog(@"%@, %@, %f", p.localizedDescription, p.localizedAdditionalDescription, p.fractionCompleted);
  • 文件上传
// 1.创建AFN管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    // 2.利用AFN管理者发送请求
    [manager POST:@"http://120.25.226.186:32812/upload" parameters:@{@"username" : @"lnj"} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    // formData是专门用于保存需要上传文件的二进制数据
        NSData *data = [NSData dataWithContentsOfFile:@"/Users/NJ-Lee/Desktop/Snip20150811_1.png"];
        /*
         第一个参数: 需要上传的文件二进制
         第二个参数: 服务器对应的参数名称
         第三个参数: 文件的名称
         第四个参数: 文件的MIME类型
         */
        [formData appendPartWithFileData:data name:@"file" fileName:@"lnj.png" mimeType:@"image/png"];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
          NSLog(@"请求成功---%@", responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"请求失败---%@", error);
    }];
 // 第一个参数: 需要上传的文件的URL
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/NJ-Lee/Desktop/Snip20150811_1.png"] name:@"file" fileName:@"lnj.png" mimeType:@"image/png" error:nil];
// 如果使用以下方法上传文件, AFN会自动获取文件的名称和类型
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/NJ-Lee/Desktop/Snip20150811_1.png"] name:@"file" error:nil];

  • AFNSerializer
  • AFN序列化:指定序列化格式,按照指定格式处理服务器返回数据,如果服务器返回格式跟指定格式不一样,会报错

    • AFN默认会将服务器返回的数据当做JSON处理,如果服务器返回的是JSON数据, 那么会自动转换为OC对象;
  • 只要给AFN的responseSerializer属性, 赋值为AFXMLParserResponseSerializer, AFN就会将服务器返回的数据当做XML来处理

manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
// 只要设置AFN的responseSerializer为XML, 那么返回的responseObject就是NSXMLParser解析器
  • 只要给AFN的responseSerializer属性, 赋值为AFHTTPResponseSerializer, 以后AFN就会将服务器返回的数据当做二进制来处理, 也就是说不做任何处理
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

十一 网络监测

  • AFN
    // 1.创建网络监听对象
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    // 2.设置网络状态改变回调
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        /*
         AFNetworkReachabilityStatusUnknown         = -1, // 未知
         AFNetworkReachabilityStatusNotReachable     = 0, // 无连接
         AFNetworkReachabilityStatusReachableViaWWAN = 1, // 3G 花钱
         AFNetworkReachabilityStatusReachableViaWiFi = 2, // 局域网络,不花钱
         */
        switch (status) {
            case 0:
                NSLog(@"无连接");
                break;
            case 1:
                NSLog(@"3G 花钱");
                break;
            case 2:
                NSLog(@"局域网络,不花钱");
                break;
            default:
                NSLog(@"未知");
                break;
        }
    }];

    // 3.开始监听
    [manager startMonitoring];
  • 苹果自带 Reachability
    - (void)viewDidLoad {
    [super viewDidLoad];

    // 创建Reachability对象
    Reachability *r1 = [Reachability reachabilityForInternetConnection];
    // 给Reachability对象注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNetworkStatus) name:kReachabilityChangedNotification object:nil];

    // 开始监听网络
    self.reachability = [Reachability reachabilityForInternetConnection];
    [self.reachability startNotifier];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.reachability stopNotifier];
}
- (void)getNetworkStatus{
    if ([Reachability reachabilityForLocalWiFi].currentReachabilityStatus != NotReachable) {
        NSLog(@"wifi");
    }else if ([Reachability reachabilityForInternetConnection].currentReachabilityStatus != NotReachable)
    {
        NSLog(@"手机自带网络");
    }else
    {
        NSLog(@"没有网络");
    }
}

十二 AFN推荐使用方式

  • AFN解耦
    • 自定义单利类基础Manager
    • 优点: 替换框架只需要求改单利类即可
+ (instancetype)shareNetworkTools
{
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 注意: 在指定baseURL的时候, 后面需要加上/
        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/"];
        instance = [[self alloc] initWithBaseURL:url sessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    });
    return instance;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值