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 ...
随机推荐
- Android Studio快捷键每日一练(1)
原文地址:http://www.developerphil.com/android-studio-tips-of-the-day-roundup-1/ 1.高亮显示相同的字符串 苹果:Cmd+shif ...
- ADO.NET封装的SqlHelper
参照别人的方法,顺便再次复习下ADO.NET的相关知识.为自己的类库做准备. namespace Common.SqlHelper { /// <summary> /// ADO.NET- ...
- 开源的即时通讯框架 (endv.cn) (一)
先实现几个常用基本功能, 1.富文本编辑器.文字的发送与接收 2.表情选择.插入.发送.读取 3.截图的插入.发送.接收 4.视频的获取.发送.接收 5.内存垃圾回收 客户端模拟服务端发送与接收 源码 ...
- Android客户端消息推送原理简介
首先简单介绍一下Android消息推送的主要三种方式,如果你已经看过类似的文章,请直接忽略三种介绍. 1.使用SMS服务,即服务器端发送短信,然后手机客户端监听短信的广播,然后对数据进行一定的处 ...
- 遗传算法的简单应用-巡回旅行商(TSP)问题的求解
上篇我们用遗传算法求解了方程,其中用到的编码方式是二进制的编码,实现起来相对简单很多, 就连交配和变异等操作也是比较简单,但是对于TSP问题,就稍微复杂一点,需要有一定的策略, 才能较好的实现. 这次 ...
- log4net的配置详解
log4net是一款优秀的第三方日志框架,可以很容易的加载到开发项目中(引用log4net的dll,再配置些基本参数即可),帮助程序员把日志信息输出到各种不同的目标,常见的有文本.数据库.window ...
- HtmlAgilityPack 删除script、style以及注释标签
foreach(var script in doc.DocumentNode.Descendants("script").ToArray()) script.Remove(); f ...
- Web API应用架构在Winform混合框架中的应用(4)--利用代码生成工具快速开发整套应用
前面几篇介绍了Web API的基础信息,以及如何基于混合框架的方式在WInform界面里面整合了Web API的接入方式,虽然我们看似调用过程比较复杂,但是基于整个框架的支持和考虑,我们提供了代码生成 ...
- 从C#到Objective-C,循序渐进学习苹果开发(3)--分类(category)和协议Protocal的理解
本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本文继续上一篇随笔<从 ...
- 他答对一半(打一字)asp.net开源简答题项目
先出个字谜: 他答对一半(打一字) 你猜出来了没? 可以到这个网址答题:http://m.hovertree.com/miyu/bjae/4fpmm2td.htm 看你的答案是否正确. 这是ASP.N ...