网络

2016-11-07  本文已影响90人  DDY
Dream.png

https://github.com/starainDou 欢迎点星

AFNetWorking

// AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; //推荐下面方式
AFHTTPSessionManager *manager =[[AFHTTPSessionManager alloc] initWithBaseURL:BaseURL];
// 设置请求头
[manager.requestSerializer setValue:@"en" forHTTPHeaderField:@"locale"];
// 请求序列化
manager.requestSerializer = [AFJSONRequestSerializer serializer];
// 设置超时时间
manager.requestSerializer.timeoutInterval = 30.f;
// 响应内容序列化
manager.responseSerializer = [AFJSONResponseSerializer serializer];
// 支持的内容类型
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", @"text/json", @"text/plain",@"text/html", nil];
// 固定参数可这样写
NSDictionary *params = @{@"uid":userId,  @"token":token};
// 可选参数可这样写
NSMutableDictionary *params = [NSMutableDictionary dictionary];
if (userId) params[@"uid"] = userId;
if (token)  params[@"token"] = token;
// 网络请求
[manager POST:url parameters:params constructingBodyWithBlock:^(id formData) {
            [formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.5)
                                        name:@"uploadfile"
                                    fileName: fileName
                                    mimeType:@"image/jpeg"];
        } progress:^(NSProgress *uploadProgress) {
            
        } success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"success : %@", responseObject);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"error : %@",error.localizedDescription);
        }];

NSURLSession

    //创建 NSURLSession 对象
    NSURLSession *session = [NSURLSession sharedSession];
    /**
     根据对象创建 Task 请求
     url  方法内部会自动将 URL 包装成一个请求对象(默认是 GET 请求)
     completionHandler  完成之后的回调(成功或失败)
     param data     返回的数据(响应体)
     param response 响应头
     param error    错误信息
     */
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:
                                      ^(NSData *data, NSURLResponse *response, NSError *error) {
                                          //解析服务器返回的数据
                                          NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
                                          //默认在子线程中解析数据
                                          NSLog(@"%@", [NSThread currentThread]);
                                      }];
    //发送请求(执行Task)
    [dataTask resume];
    NSString *str = @"http://doudianyu.com/commetlist/";
    // 如果需要中文编码
    NSString *urlPath = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //确定请求路径
    NSURL *url = [NSURL URLWithString:urlPath];
    //创建可变请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //修改请求方法
    request.HTTPMethod = @"POST";
    // 设置请求头
    [request setValue:@"en" forHTTPHeaderField:@"local"];
    //设置请求体
    request.HTTPBody = [@"page=0&size=50&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
    //创建会话对象
    NSURLSession *session = [NSURLSession sharedSession];
    // 遮罩
    [SVProgressHUD show];
    //创建请求 Task
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:
                                      ^(NSData *data, NSURLResponse *response, NSError *error) {
                                          //回到主线程更新UI -> 撤销遮罩
                                          dispatch_async(dispatch_get_main_queue(), ^{
                                              [SVProgressHUD dismiss];
                                          });
                                          
                                          if (error) {
                                              NSLog(@"提示用户请求失败...error:%@",error);
                                          } else {
                                              // JSON解析 苹果原生效率很高
                                              NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
                                              if ([[result objectForKey:@"message"] isEqualToString:@"success"]) {
                                                  //获取数据->主线程更新UI
                                                  dispatch_async(dispatch_get_main_queue(), ^{
                                                      NSDictionary *data = [result objectForKey:@"data"];
                                                  });
                                              }else{
                                                  NSLog(@"未查到信息....");
                                              }
                                              NSLog(@"请求成功... %@",result);
                                          }
                                      }];
    //发送请求
    [dataTask resume];
//确定请求路径
 NSURL *url = [NSURL URLWithString:@"http://doudianyu.com/commetlist/"];
 //创建可变请求对象
 NSMutableURLRequest *requestM = [NSMutableURLRequest requestWithURL:url];
 //设置请求方法
 requestM.HTTPMethod = @"POST";
 //设置请求体
 requestM.HTTPBody = [@"page=0&size=50&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
 //创建会话对象,设置代理
 /**
  第一个参数:配置信息
  第二个参数:设置代理
  第三个参数:队列,如果该参数传递nil 那么默认在子线程中执行
  */
 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
                              delegate:self delegateQueue:nil];
 //创建请求 Task
 NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:requestM];
 //发送请求
 [dataTask resume];

4.遵守协议,实现代理方法(常用的有三种代理方法)

#pragma mark 接收到服务器响应的时候调用
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
     NSLog(@"子线程中执行 -- %@", [NSThread currentThread]);
     self.dataM = [NSMutableData data];
     // 默认情况下不接收数据,必须告诉系统是否接收服务器返回的数据
     completionHandler(NSURLSessionResponseAllow);
}
#pragma mark 接受到服务器返回数据的时候调用,可能被调用多次
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
     //拼接服务器返回的数据
     [self.dataM appendData:data];
}
#pragma mark 请求完成或者是失败的时候调用
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
     //解析服务器返回数据
     NSLog(@"%@", [[NSString alloc] initWithData:self.dataM encoding:NSUTF8StringEncoding]);
}

5.设置代理之后的强引用问题

-(void)dealloc
{
    //注意:在不用的时候一定要调用该方法来释放,不然会出现内存泄露问题
    //方法一:取消所有过去的会话和任务
    [self.session invalidateAndCancel];
    //方法二:可在释放时做一些操作
    [self.session resetWithCompletionHandler:^{
         // 释放时做的操作
    }];
}
[[[NSURLSession sharedSession] dataTaskWithURL:imgURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
     dispatch_async(dispatch_get_main_queue(), ^{
         self.imageView.image =  [UIImage imageWithData:data];
     });
 }] resume];
 //优点:该方法内部已经完成了边接收数据边写沙盒的操作,解决了内存飙升的问题
 NSURLSessionDownloadTask *downTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
     // 默认存储到临时文件夹 tmp 中,需要剪切文件到 cache
     NSLog(@"目标位置%@", location);
     NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];
     // fileURLWithPath:有协议头; URLWithString:无协议头
     [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];
 }];
 //发送请求
 [downTask resume];

NSURLConnection

FileUpload.h

#import <Foundation/Foundation.h>

@interface FileUpload : NSObject

@end

FileUpload.m

// iOS post方式上传文件

#import "FileUpload.h"

@implementation FileUpload

+ (void)uploadFiles:(NSArray *)files msgId:(NSString *)msgId obj:(id)obj userid:(NSString *)userid {
    // 分界线的标识符
    for (NSMutableDictionary *filedic in files) {
        NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
        // 根据url初始化request
        NSString* URL = [NSString stringWithFormat:@"http://%@%@",NSLocalizedString(@"MQTT_IP", @""),NSLocalizedString(@"im_uploadfileURL", @"")];
        NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]
                                                               cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                           timeoutInterval:10];
        // 分界线 --AaB03x
        NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
        // 结束符 AaB03x--
        NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
        // 要上传的文件
        NSData *data = [NSData dataWithContentsOfFile:[filedic objectForKey:@"filepath"]];
        
        // http body的字符串
        NSMutableString *body=[[NSMutableString alloc]init];
        // 参数的集合普通的key-value参数
        
        body = [self setParamsKey:@"uptype" value:@"1" body:body];
        body = [self setParamsKey:@"sid" value:msgId body:body];
        body = [self setParamsKey:@"uid" value:userid body:body];
        
        // 添加分界线,换行
        [body appendFormat:@"%@\r\n",MPboundary];
        // 声明文件字段,文件名
        [body appendFormat:@"Content-Disposition: form-data; name=\"upfile\"; filename=\"%@\"\r\n",[filedic objectForKey:@"serverfilename"]];
        // 声明上传文件的格式 
        [body appendFormat:@"Content-Type: %@\r\n\r\n",[self GetContentType:[filedic objectForKey:@"serverfilename"]]];
        
        // 声明结束符:--AaB03x--
        NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
        // 声明myRequestData,用来放入http body
        NSMutableData *myRequestData=[NSMutableData data];
        // 将body字符串转化为UTF8格式的二进制
        [myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
        // 将file的data加入
        [myRequestData appendData:data];
        // 加入结束符--AaB03x--
        [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
        
        // 设置HTTPHeader中Content-Type的值
        NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
        // 设置HTTPHeader
        [request setValue:content forHTTPHeaderField:@"Content-Type"];
        // 设置Content-Length
        [request setValue:[NSString stringWithFormat:@"%lu", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
        // 设置http body
        [request setHTTPBody:myRequestData];
        // http method
        [request setHTTPMethod:@"POST"];
        
        // 开线程下载
        dispatch_queue_t defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(defaultQueue, ^{
            // 另开线程
            NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
            NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
            NSLog(@"上传状态返回值: %@", returnString);
            //                   if ([returnString isEqualToString:@"0"]) {
            //                       [self responseLoadFinish:files msgId:msgId upload:upload];
            //                } else {
            //                        [self responseLoadFail:files msgId:msgId upload:upload];
            //                    }
        });
    }
}

+ (NSMutableString*)setParamsKey:(NSString*)key value:(NSString*)value body:(NSMutableString*)body {
    NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
    // 分界线 --AaB03x
    NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
    // 添加分界线,换行
    [body appendFormat:@"%@\r\n",MPboundary];
    // 添加字段名称,换2行
    [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
    // 添加字段的值
    [body appendFormat:@"%@\r\n",value];
    return body;
}
+ (NSString*)GetContentType:(NSString*)filename {
    if ([filename hasSuffix:@".avi"]) {
        return @"video/avi";
    }  else if ([filename hasSuffix:@".bmp"]) {
        return @"application/x-bmp";
    } else if ([filename hasSuffix:@"jpeg"]) {
        return @"image/jpeg";
    } else if ([filename hasSuffix:@"jpg"]) {
        return @"image/jpeg";
    } else if ([filename hasSuffix:@"png"]) {
        // chrome/firefox 标准image/png,IE 标准image/x-png
        return @"image/png"; 
    } else if ([filename hasSuffix:@"mp3"]) {
        return @"audio/mp3";
    } else if ([filename hasSuffix:@"mp4"]) {
        return @"video/mpeg4";
    } else if ([filename hasSuffix:@"rmvb"]) {
        return @"application/vnd.rn-realmedia-vbr";
    } else if ([filename hasSuffix:@"txt"]) {
        return @"text/plain";
    } else if ([filename hasSuffix:@"xsl"]) {
        return @"application/x-xls";
    } else if ([filename hasSuffix:@"xslx"]) {
        return @"application/x-xls";
    } else if ([filename hasSuffix:@"xwd"]) {
        return @"application/x-xwd";
    } else if ([filename hasSuffix:@"doc"]) {
        return @"application/msword";
    } else if ([filename hasSuffix:@"docx"]) {
        return @"application/msword";
    } else if ([filename hasSuffix:@"ppt"]) {
        return @"application/x-ppt";
    } else if ([filename hasSuffix:@"pdf"]) {
        return @"application/pdf";
    }
    return nil;
}
@end

原生方式监听网络状态

方法一 :command + shift + 0打开 Documentation And API reference 搜索 Reachability
方法二:到网页[下载]https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html

//  [Reachability reachabilityForInternetConnection];只能判断wifi是否可用,不能判断是否连接以太网络
Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];
NetworkStatus netStatus = [reachability currentReachabilityStatus];
    switch (netStatus) {
        case NotReachable:
            break;
        case ReachableViaWiFi:
            networkStatus = 
            break;
        case ReachableViaWWAN:
            break;

        default:
            break;
    }
- (void) addNotifacation
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
 [self.hostReachability startNotifier];
}

- (void) reachabilityChanged:(NSNotification *)note
{
 Reachability* curReach = [note object];
 NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
 NetworkStatus netStatus = [reachability currentReachabilityStatus];
}

AFNetworking 中封装的监听网络

// 1.获得网络监控的管理者
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
// 2.设置网络状态改变后的处理
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
// 当网络状态改变了, 就会调用这个block
switch(status) {
    case AFNetworkReachabilityStatusUnknown: // 未知网络
              NSLog(@"未知网络");
              break;
     case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
              NSLog(@"没有网络(断网)");
              break;
    case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
              NSLog(@"手机自带网络");
              break;
    case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
              NSLog(@"WIFI");
              break;
    }
}];
// 3.开始监控
[mgr startMonitoring];

AFN和ASI有啥不同

1 AFN基于NSURL(NSURLSession&NSURLConnection),ASI基于底层的CFNetwork框架,因此ASI的性能优于AFN
2 AFN采取block的方式处理请求,ASI最初采取delegate的方式处理请求,后面也增加了block的方式
3 AFN只封装了一些常用功能,满足基本需求,直接忽略了很多扩展功能,比如没有封装同步请求;ASI提供的功能较多,预留了各种接口和工具供开发者自行扩展
4 AFN直接解析服务器返回的JSON、XML等数据,而ASI比较原始,返回的是NSData二进制数据
5 AFN在iOS9.0之后需要网络权限,而ASI不需要

在真正的开发中

  1. 如果是普通的GET&POST请求、小文件上传,强烈建议用AFN,因为AFN简单好用。

  2. 如果是下载强烈建议用ASI,因为它提供了很强大的功能。

附:

上一篇下一篇

猜你喜欢

热点阅读