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

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

具体他的pod的过过程

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

代码如下

#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self Upload];
// [self dataTask];
// [self MultiUpload];
// [self Serialization];
// [self PostMethod];
// [self Reacheab]; }
//下载
-(void)Download{
NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration]; NSURL *URL=[NSURL URLWithString:@"example/download"];
NSURLRequest *request=[NSURLRequest requestWithURL:URL]; NSURLSessionDownloadTask *downloadTask=[manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL=[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
NSLog(@"file downloaded to :%@",filePath);
}];
[downloadTask resume]; }
// 上传
-(void)Upload{
NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration]; NSURL *url=[NSURL URLWithString:@"example/upload.php"]; NSURLRequest *request=[NSURLRequest requestWithURL:url]; NSURL *filePath=[NSURL fileURLWithPath:@"path/aa.txt"]; NSURLSessionUploadTask *uploadTask=[manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Errof:%@",error);
}else{
NSLog(@"Success:%@ %@",response,responseObject);
}
}];
[uploadTask resume];
} -(void)MultiUpload{ NSMutableURLRequest *request=[[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"https:example/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"path/1.png"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
} error:nil]; AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; NSURLSessionUploadTask *uploadTask; uploadTask=[manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIProgressView new] setProgress:uploadProgress.fractionCompleted];
});
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"errof:%@",error);
}else{
NSLog(@"%@ %@",response,responseObject);
}
}]; [uploadTask resume];
}
// data Task
-(void)dataTask{
NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration]; NSURL *url=[NSURL URLWithString:@"http://1.studyios.sinaapp.com/gyxy.php?a=qq"]; NSURLRequest *request=[NSURLRequest requestWithURL:url]; NSURLSessionDataTask *dataTask=[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@",error);
}else{
NSLog(@"%@ %@",response,responseObject);
}
}]; [dataTask resume];
}
//GET方法 -(void)Serialization{
NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
NSString *url=@"http://1.studyios.sinaapp.com/gyxy.php";
NSDictionary *parameters=@{@"a":@"BB",@"b":@"CC",@"c":@"aa"};
NSMutableURLRequest *request= [[AFHTTPRequestSerializer serializer]requestWithMethod:@"GET" URLString:url parameters:parameters error:nil]; NSURLSessionDataTask *dataTask=[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@",error);
}else{
NSLog(@"%@",responseObject);
}
}];
[dataTask resume]; }
//POST
-(void)PostMethod{
NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
NSString *url=@"http://1.studyios.sinaapp.com/mypost.php";
NSDictionary *dic=@{@"can1":@"abc",@"can2":@"bcd"};
NSMutableURLRequest *request=[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:dic error:nil];
//
// NSURLSessionDataTask *dataTask=[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@",error);
}else{
// NSLog(@"%@",responseObject);
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@",dic);
}
}];
[dataTask resume]; } -(void)Reacheab{ [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"reacheability:%@",AFStringFromNetworkReachabilityStatus(status));
}];
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
-(void)SSLCertificates{
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
manager.securityPolicy.allowInvalidCertificates=YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @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. C#对图片文件的压缩、裁剪操作初探

    在做项目时,对图片的处理,以前都采用在上传时,限制其大小的方式,这样带来诸多不便.毕竟网站运维人员不一定会对图片做处理,经常超出大小限制,即使会使用图片处理软件的,也由于个人水平方面原因,处理效果差强 ...

  2. 在Windows Phone 8中使用Live Connect并保持登陆状态

    Live Connect可以让各种客户端访问Live账号.获取好友列表.访问One Drive的文件等,官方地址在此:http://msdn.microsoft.com/zh-cn/live/ff51 ...

  3. .NET Framework介绍

    .NET Framework 是一个集成在 Windows 中的组件,它支持生成和运行下一代应用程序与 XML Web Services. .NET Framework 旨在实现下列目标: 提供一个一 ...

  4. autofac 使用

    var builder = new ContainerBuilder();var container = builder.Build(); var assemblies = new Directory ...

  5. Cygwin的安装

    Android开发要用到NDK,装了一个虚拟机,老是不行. 后来安装了一个cygwin,安装完毕后unset home,再export NDK,就可以使用了,非常方便,不用像虚拟机那样经常切换.

  6. python-ldap实现登录案例

    ldap_config = { 'ldap_path': 'ldap://xx.xx.xx.xx:389', 'base_dn': 'ou=users,dc=ledo,dc=com', 'ldap_u ...

  7. 【FOL】万里长征第一步

    准备了好久了,中间断断续续看了些资料,也写了几个小demo练手,今天正式开始. 因为要模拟debug和release环境,手上资源又很缺,必须把一些已经拼好的图片进行切割,网络上找了半天倒是有几个切图 ...

  8. loadrunner录制回放常见问题及解决办法

    1.录制错误    1)录制本机 WebTours 或录制本地网站无法打开    当 使 用 Vista 以 后 的 操 作 系 统 时 , 会 出 现 该 问 题 . 这 是 由 于 在 本 地Wi ...

  9. Java基础学习小记--多态

    题外话:总结了多年的学习心得,不得不说,睡眠是一个学习者的必需品!所谓"早起毁一天"不是没有道理哪,特别对Coders来说,有几天不是加班到夜里.好吧,我承认对于初学Java的我, ...

  10. Java IO 装饰者模式

    装饰模式(Decorator) 装饰模式又名包装(Wrapper)模式. 装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式通过创建一个包装对象,也就是装饰,来包裹真实的 ...