AFNetworking 3.0.4 的使用
本文永久链接: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 的使用的更多相关文章
- AFNetworking 3.0 源码解读 总结(干货)(下)
承接上一篇AFNetworking 3.0 源码解读 总结(干货)(上) 21.网络服务类型NSURLRequestNetworkServiceType 示例代码: typedef NS_ENUM(N ...
- AFNetworking 3.0 源码解读(十一)之 UIButton/UIProgressView/UIWebView + AFNetworking
AFNetworking的源码解读马上就结束了,这一篇应该算是倒数第二篇,下一篇会是对AFNetworking中的技术点进行总结. 前言 上一篇我们总结了 UIActivityIndicatorVie ...
- AFNetworking 3.0 源码解读(十)之 UIActivityIndicatorView/UIRefreshControl/UIImageView + AFNetworking
我们应该看到过很多类似这样的例子:某个控件拥有加载网络图片的能力.但这究竟是怎么做到的呢?看完这篇文章就明白了. 前言 这篇我们会介绍 AFNetworking 中的3个UIKit中的分类.UIAct ...
- AFNetworking 3.0 源码解读(九)之 AFNetworkActivityIndicatorManager
让我们的APP像艺术品一样优雅,开发工程师更像是一名匠人,不仅需要精湛的技艺,而且要有一颗匠心. 前言 AFNetworkActivityIndicatorManager 是对状态栏中网络激活那个小控 ...
- AFNetworking 3.0 源码解读(八)之 AFImageDownloader
AFImageDownloader 这个类对写DownloadManager有很大的借鉴意义.在平时的开发中,当我们使用UIImageView加载一个网络上的图片时,其原理就是把图片下载下来,然后再赋 ...
- AFNetworking 3.0 源码解读(七)之 AFAutoPurgingImageCache
这篇我们就要介绍AFAutoPurgingImageCache这个类了.这个类给了我们临时管理图片内存的能力. 前言 假如说我们要写一个通用的网络框架,除了必备的请求数据的方法外,必须提供一个下载器来 ...
- AFNetworking 3.0 源码解读(六)之 AFHTTPSessionManager
AFHTTPSessionManager相对来说比较好理解,代码也比较短.但却是我们平时可能使用最多的类. AFNetworking 3.0 源码解读(一)之 AFNetworkReachabilit ...
- AFNetworking 3.0 源码解读(三)之 AFURLRequestSerialization
这篇就讲到了跟请求相关的类了 关于AFNetworking 3.0 源码解读 的文章篇幅都会很长,因为不仅仅要把代码进行详细的的解释,还会大概讲解和代码相关的知识点. 上半篇: URI编码的知识 关于 ...
- AFNetworking 3.0 源码解读(四)之 AFURLResponseSerialization
本篇是AFNetworking 3.0 源码解读的第四篇了. AFNetworking 3.0 源码解读(一)之 AFNetworkReachabilityManager AFNetworking 3 ...
- AFNetworking 3.0 源码解读(五)之 AFURLSessionManager
本篇是AFNetworking 3.0 源码解读的第五篇了. AFNetworking 3.0 源码解读(一)之 AFNetworkReachabilityManager AFNetworking 3 ...
随机推荐
- SQL Server时间粒度系列----第2节日期、周时间粒度详解
本文目录列表: 1.从MySQL提供的TO_DAYS和FROM_DAYS这对函数说起2.SQL Server日期时间粒度3.SQL Server周有关时间粒度 4.总结语 5.参考清单列表 从My ...
- SyteLine实现字段过滤
如果说某个下拉列表,想过滤某些选项,该怎样来做呢,在Infor Syteline实现起来是一件很容易的事情.如在Fixed Assets窗口中的Class Code的下拉列表中,如果不想"_ ...
- win10下vs2015创建asp,net core项目并运行在ubuntu14.04下
上文说了.net core程序在win10与ubuntu下运行,用的是示例程序(https://github.com/aspnet/cli-samples),今天用vs2015 构建asp.net c ...
- linux命令笔记
3步实现ssh login不用密码: 1 ssh-keygen (Create public and private keys) 2 ssh-copy-id -i ~/.ssh/id_rsa.pub ...
- SSH实例(6)
在WebContent文件夹下新建query.jsp和save.jsp文件. query.jsp: <%@ page language="java" import=" ...
- 使用exe4j打包Java程序
工具: exe4j软件(云盘存) 可以运行的Java程序的jar包 打开我们已经安装好的exe4j软件,首先看到的是一个欢迎界面,我们直接[next]就可以了: 2 在第二步中我们选择[JAR in ...
- Lucene.net站内搜索—1、SEO优化
目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...
- 一行代码调用实现带字段选取+条件判断+排序+分页功能的增强ORM框架
问题:3行代码 PDF.NET是一个开源的数据开发框架,它的特点是简单.轻量.快速,易上手,而且是一个注释完善的国产开发框架,受到不少朋友的欢迎,也在我们公司的项目中多次使用.但是,PDF.NET比起 ...
- jquery+jplayer实现歌词同步的mp3音乐播放器效果
实例预览 下载地址 实例代码 <div class="container"> <div class="demo"> <textar ...
- 值得 Web 开发人员收藏的20个 HTML5 实例教程
当开始学习如何创建 Web 应用程序或网站的时候,最流行的建议之一就是阅读教程,并付诸实践.也有大量的 Web 开发的书,但光有理论没有实际行动是无用的.现在由于网络的发展,我们有很多的工具可以用于创 ...