本文永久链接:http://www.cnblogs.com/qianLL/p/5342593.html

pod 'AFNetworking', '~>3.0.4'    <-------第三方

具体他的pod的过过程

http://www.cnblogs.com/qianLL/p/5331624.html

代码如下

  1. #import "ViewController.h"
  2. #import "AFNetworking.h"
  3. @interface ViewController ()
  4.  
  5. @end
  6.  
  7. @implementation ViewController
  8.  
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. [self Upload];
  12. // [self dataTask];
  13. // [self MultiUpload];
  14. // [self Serialization];
  15. // [self PostMethod];
  16. // [self Reacheab];
  17.  
  18. }
  19. //下载
  20. -(void)Download{
  21. NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration];
  22.  
  23. AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
  24.  
  25. NSURL *URL=[NSURL URLWithString:@"example/download"];
  26. NSURLRequest *request=[NSURLRequest requestWithURL:URL];
  27.  
  28. NSURLSessionDownloadTask *downloadTask=[manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
  29. NSURL *documentsDirectoryURL=[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  30. return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
  31. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  32. NSLog(@"file downloaded to :%@",filePath);
  33. }];
  34. [downloadTask resume];
  35.  
  36. }
  37. // 上传
  38. -(void)Upload{
  39. NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration];
  40.  
  41. AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
  42.  
  43. NSURL *url=[NSURL URLWithString:@"example/upload.php"];
  44.  
  45. NSURLRequest *request=[NSURLRequest requestWithURL:url];
  46.  
  47. NSURL *filePath=[NSURL fileURLWithPath:@"path/aa.txt"];
  48.  
  49. NSURLSessionUploadTask *uploadTask=[manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  50. if (error) {
  51. NSLog(@"Errof:%@",error);
  52. }else{
  53. NSLog(@"Success:%@ %@",response,responseObject);
  54. }
  55. }];
  56. [uploadTask resume];
  57. }
  58.  
  59. -(void)MultiUpload{
  60.  
  61. NSMutableURLRequest *request=[[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"https:example/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
  62. [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"path/1.png"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
  63. } error:nil];
  64.  
  65. AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  66.  
  67. NSURLSessionUploadTask *uploadTask;
  68.  
  69. uploadTask=[manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
  70. dispatch_async(dispatch_get_main_queue(), ^{
  71. [[UIProgressView new] setProgress:uploadProgress.fractionCompleted];
  72. });
  73. } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  74. if (error) {
  75. NSLog(@"errof:%@",error);
  76. }else{
  77. NSLog(@"%@ %@",response,responseObject);
  78. }
  79. }];
  80.  
  81. [uploadTask resume];
  82. }
  83. // data Task
  84. -(void)dataTask{
  85. NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration];
  86.  
  87. AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
  88.  
  89. NSURL *url=[NSURL URLWithString:@"http://1.studyios.sinaapp.com/gyxy.php?a=qq"];
  90.  
  91. NSURLRequest *request=[NSURLRequest requestWithURL:url];
  92.  
  93. NSURLSessionDataTask *dataTask=[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  94. if (error) {
  95. NSLog(@"Error: %@",error);
  96. }else{
  97. NSLog(@"%@ %@",response,responseObject);
  98. }
  99. }];
  100.  
  101. [dataTask resume];
  102. }
  103. //GET方法
  104.  
  105. -(void)Serialization{
  106. NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration];
  107.  
  108. AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
  109. NSString *url=@"http://1.studyios.sinaapp.com/gyxy.php";
  110. NSDictionary *parameters=@{@"a":@"BB",@"b":@"CC",@"c":@"aa"};
  111. NSMutableURLRequest *request= [[AFHTTPRequestSerializer serializer]requestWithMethod:@"GET" URLString:url parameters:parameters error:nil];
  112.  
  113. NSURLSessionDataTask *dataTask=[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  114. if (error) {
  115. NSLog(@"Error: %@",error);
  116. }else{
  117. NSLog(@"%@",responseObject);
  118. }
  119. }];
  120. [dataTask resume];
  121.  
  122. }
  123. //POST
  124. -(void)PostMethod{
  125. NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration];
  126. AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
  127. NSString *url=@"http://1.studyios.sinaapp.com/mypost.php";
  128. NSDictionary *dic=@{@"can1":@"abc",@"can2":@"bcd"};
  129. NSMutableURLRequest *request=[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:dic error:nil];
  130. //
  131. //
  132.  
  133. NSURLSessionDataTask *dataTask=[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  134. if (error) {
  135. NSLog(@"Error: %@",error);
  136. }else{
  137. // NSLog(@"%@",responseObject);
  138. NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
  139. NSLog(@"%@",dic);
  140. }
  141. }];
  142. [dataTask resume];
  143.  
  144. }
  145.  
  146. -(void)Reacheab{
  147.  
  148. [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  149. NSLog(@"reacheability:%@",AFStringFromNetworkReachabilityStatus(status));
  150. }];
  151. [[AFNetworkReachabilityManager sharedManager] startMonitoring];
  152. }
  153. -(void)SSLCertificates{
  154. AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
  155. manager.securityPolicy.allowInvalidCertificates=YES;
  156. }
  157. - (void)didReceiveMemoryWarning {
  158. [super didReceiveMemoryWarning];
  159. // Dispose of any resources that can be recreated.
  160. }
  161.  
  162. @end

AFNetworking 3.0.4 的使用的更多相关文章

  1. AFNetworking 3.0 源码解读 总结(干货)(下)

    承接上一篇AFNetworking 3.0 源码解读 总结(干货)(上) 21.网络服务类型NSURLRequestNetworkServiceType 示例代码: typedef NS_ENUM(N ...

  2. AFNetworking 3.0 源码解读(十一)之 UIButton/UIProgressView/UIWebView + AFNetworking

    AFNetworking的源码解读马上就结束了,这一篇应该算是倒数第二篇,下一篇会是对AFNetworking中的技术点进行总结. 前言 上一篇我们总结了 UIActivityIndicatorVie ...

  3. AFNetworking 3.0 源码解读(十)之 UIActivityIndicatorView/UIRefreshControl/UIImageView + AFNetworking

    我们应该看到过很多类似这样的例子:某个控件拥有加载网络图片的能力.但这究竟是怎么做到的呢?看完这篇文章就明白了. 前言 这篇我们会介绍 AFNetworking 中的3个UIKit中的分类.UIAct ...

  4. AFNetworking 3.0 源码解读(九)之 AFNetworkActivityIndicatorManager

    让我们的APP像艺术品一样优雅,开发工程师更像是一名匠人,不仅需要精湛的技艺,而且要有一颗匠心. 前言 AFNetworkActivityIndicatorManager 是对状态栏中网络激活那个小控 ...

  5. AFNetworking 3.0 源码解读(八)之 AFImageDownloader

    AFImageDownloader 这个类对写DownloadManager有很大的借鉴意义.在平时的开发中,当我们使用UIImageView加载一个网络上的图片时,其原理就是把图片下载下来,然后再赋 ...

  6. AFNetworking 3.0 源码解读(七)之 AFAutoPurgingImageCache

    这篇我们就要介绍AFAutoPurgingImageCache这个类了.这个类给了我们临时管理图片内存的能力. 前言 假如说我们要写一个通用的网络框架,除了必备的请求数据的方法外,必须提供一个下载器来 ...

  7. AFNetworking 3.0 源码解读(六)之 AFHTTPSessionManager

    AFHTTPSessionManager相对来说比较好理解,代码也比较短.但却是我们平时可能使用最多的类. AFNetworking 3.0 源码解读(一)之 AFNetworkReachabilit ...

  8. AFNetworking 3.0 源码解读(三)之 AFURLRequestSerialization

    这篇就讲到了跟请求相关的类了 关于AFNetworking 3.0 源码解读 的文章篇幅都会很长,因为不仅仅要把代码进行详细的的解释,还会大概讲解和代码相关的知识点. 上半篇: URI编码的知识 关于 ...

  9. AFNetworking 3.0 源码解读(四)之 AFURLResponseSerialization

    本篇是AFNetworking 3.0 源码解读的第四篇了. AFNetworking 3.0 源码解读(一)之 AFNetworkReachabilityManager AFNetworking 3 ...

  10. AFNetworking 3.0 源码解读(五)之 AFURLSessionManager

    本篇是AFNetworking 3.0 源码解读的第五篇了. AFNetworking 3.0 源码解读(一)之 AFNetworkReachabilityManager AFNetworking 3 ...

随机推荐

  1. Android Studio快捷键每日一练(1)

    原文地址:http://www.developerphil.com/android-studio-tips-of-the-day-roundup-1/ 1.高亮显示相同的字符串 苹果:Cmd+shif ...

  2. ADO.NET封装的SqlHelper

    参照别人的方法,顺便再次复习下ADO.NET的相关知识.为自己的类库做准备. namespace Common.SqlHelper { /// <summary> /// ADO.NET- ...

  3. 开源的即时通讯框架 (endv.cn) (一)

    先实现几个常用基本功能, 1.富文本编辑器.文字的发送与接收 2.表情选择.插入.发送.读取 3.截图的插入.发送.接收 4.视频的获取.发送.接收 5.内存垃圾回收 客户端模拟服务端发送与接收 源码 ...

  4. Android客户端消息推送原理简介

    首先简单介绍一下Android消息推送的主要三种方式,如果你已经看过类似的文章,请直接忽略三种介绍.    1.使用SMS服务,即服务器端发送短信,然后手机客户端监听短信的广播,然后对数据进行一定的处 ...

  5. 遗传算法的简单应用-巡回旅行商(TSP)问题的求解

    上篇我们用遗传算法求解了方程,其中用到的编码方式是二进制的编码,实现起来相对简单很多, 就连交配和变异等操作也是比较简单,但是对于TSP问题,就稍微复杂一点,需要有一定的策略, 才能较好的实现. 这次 ...

  6. log4net的配置详解

    log4net是一款优秀的第三方日志框架,可以很容易的加载到开发项目中(引用log4net的dll,再配置些基本参数即可),帮助程序员把日志信息输出到各种不同的目标,常见的有文本.数据库.window ...

  7. HtmlAgilityPack 删除script、style以及注释标签

    foreach(var script in doc.DocumentNode.Descendants("script").ToArray()) script.Remove(); f ...

  8. Web API应用架构在Winform混合框架中的应用(4)--利用代码生成工具快速开发整套应用

    前面几篇介绍了Web API的基础信息,以及如何基于混合框架的方式在WInform界面里面整合了Web API的接入方式,虽然我们看似调用过程比较复杂,但是基于整个框架的支持和考虑,我们提供了代码生成 ...

  9. 从C#到Objective-C,循序渐进学习苹果开发(3)--分类(category)和协议Protocal的理解

    本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本文继续上一篇随笔<从 ...

  10. 他答对一半(打一字)asp.net开源简答题项目

    先出个字谜: 他答对一半(打一字) 你猜出来了没? 可以到这个网址答题:http://m.hovertree.com/miyu/bjae/4fpmm2td.htm 看你的答案是否正确. 这是ASP.N ...