AFNetworking 3.0.4 的使用,afnetworking3.0.4

來源:互聯網
上載者:User

AFNetworking 3.0.4 的使用,afnetworking3.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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.