-
AFNetwork 作用和用法详解
AFNetworking是一个轻量级的iOS网络通信类库。它建立在NSURLConnection和NSOperation等类库的基础上,让很多网络通信功能的实现变得十分简单。它支持HTTP请求和基于REST的网络服务(包括GET、POST、 PUT、DELETE等)。支持ARC。Github地址:https://github.com/AFNetworking/AFNetworking
-
- #import "MJViewController.h"
- #import "AFNetworking.h"
-
- @interface MJViewController ()
-
- @end
-
- @implementation MJViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- [self reach];
- }
-
- #pragma mark - 演练
- #pragma mark - 检测网络连接
- - (void)reach
- {
-
-
- [[AFNetworkReachabilityManager sharedManager] startMonitoring];
-
-
- [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
- NSLog(@"%d", status);
- }];
- }
-
- #pragma mark - Session 下载
- - (void)sessionDownload
- {
- NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
- AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:config];
-
- NSString *urlString = @"http://localhost/itcast/videos/01.C语言-语法预览.mp4";
- urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
-
- NSURL *url = [NSURL URLWithString:urlString];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
- NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
-
-
- NSString *cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
- NSString *path = [cacheDir stringByAppendingPathComponent:response.suggestedFilename];
-
-
- NSURL *fileURL1 = [NSURL URLWithString:path];
- NSURL *fileURL = [NSURL fileURLWithPath:path];
-
- NSLog(@"== %@ |||| %@", fileURL1, fileURL);
-
- return fileURL;
- } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
- NSLog(@"%@ %@", filePath, error);
- }];
-
- [task resume];
- }
- #pragma mark - POST JSON
- - (void)postJSON
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
- NSDictionary *dict = @{@"name": @"zhangsan"};
- NSDictionary *dict1 = @{@"name": @"wangwu"};
- NSArray *array = @[dict, dict1];
-
- manager.requestSerializer = [AFJSONRequestSerializer serializer];
-
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
-
- [manager POST:@"http://localhost/postjson.php" parameters:array success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
-
- NSLog(@"%@", result);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
-
- }];
-
- }
-
- #pragma mark - 随机文件名上传
- - (void)postUpload1
- {
-
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
-
- [manager POST:@"http://localhost/demo/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
-
- NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"头像1.png" withExtension:nil];
-
-
-
-
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
-
- formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
- NSString *fileName = [formatter stringFromDate:[NSDate date]];
-
- [formData appendPartWithFileURL:fileURL name:@"uploadFile" fileName:fileName mimeType:@"image/png" error:NULL];
-
- } success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"OK");
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"error");
- }];
- }
-
- #pragma mark - POST上传
- - (void)postUpload
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
-
-
-
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
-
-
- [manager POST:@"http://localhost/demo/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
-
-
- NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"头像1.png" withExtension:nil];
-
- [formData appendPartWithFileURL:fileURL name:@"uploadFile" error:NULL];
- } success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
-
- NSLog(@"完成 %@", result);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"错误 %@", error.localizedDescription);
- }];
- }
- #pragma mark - JSON
- - (void)XMLData
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
-
- NSDictionary *dict = @{@"format": @"xml"};
-
-
- [manager GET:@"http://localhost/videos.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
-
-
-
- NSLog(@"%@", responseObject);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
-
- #pragma mark - JSON
- - (void)JSONData
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- NSDictionary *dict = @{@"format": @"json"};
-
-
- [manager GET:@"http://localhost/videos.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"%@", responseObject);
-
-
- NSLog(@"%@", [NSThread currentThread]);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
-
- #pragma mark - POST登录
- - (void)postLogin
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- NSDictionary *dict = @{@"username": @"wangwu", @"password" : @"wang"};
-
-
- [manager POST:@"http://localhost/login.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"%@", responseObject);
-
-
- NSLog(@"%@", [NSThread currentThread]);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
-
- #pragma mark - GET登录
- - (void)getLogin
- {
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
-
-
- NSDictionary *dict = @{@"username": @"wangwu", @"password" : @"wang"};
-
-
- [manager GET:@"http://localhost/login.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"%@", responseObject);
-
-
- NSLog(@"%@", [NSThread currentThread]);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"%@", error);
- }];
- }
-
- @end
-
相关阅读:
( 转)移动端H5页面之iphone6的适配
谨慎设置iScroll4的useTransform属性,他会导致scrollToElement方法表现异常
(转)配置Apache服务器,使浏览器访问无缓存
html DIV元素左右偏移方法,偏移后默认宽度仍浏览器宽度一致
Content Security Policy(CSP)简介(转)
隐式打开Activity——Intent设置(如何打开)和Intent-fileter配置(怎么能被打开)
最近使用iScroll遇到的一些问题及最后的解决方法
(转)CSS3 Media Queries
自定义checkbox和radio
三只松鼠卖坚果
-
原文地址:https://www.cnblogs.com/xuhongwei/p/4964108.html
Copyright © 2020-2023
润新知