iOS中网络请求--AFNetworking

来源:互联网 发布:原画梦的网络课怎么样 编辑:程序博客网 时间:2024/04/28 22:08

1.打开终端,用 cd 进入新建的工程文件夹

2.在终端输入:

   touch Podfile   
   vim Podfile

    回车,然后输入 i  ,回车

输入:

     platform:ios, '9.0'

      pod 'AFNetworking', '2.6.3'

回车

3.编辑完成后按“esc”
   再按“:”,这个时候输入wq,点击回车

4.输入:

         pod install 

保存并退出终端

5.打开工程,点击 图下中第二列 第二个工程


6.选择 Info.plist文件,在Key下  ---- NSAppTransportSecurity  类型 Dictionary 下添加  NSAllowsArbitraryLoads 类型 Boolean ,值设为  YES,



////  ViewController.m//  UI_AFNetworking////  Created by lanou3g on 15/11/13.//  Copyright © 2015年 lirui. All rights reserved.//#import "ViewController.h"#import <AFNetworking.h>#import <UIImageView+AFNetworking.h>@interface ViewController ()@property (strong, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (IBAction)buttonDidClicked:(id)sender {    //实例化一个请求管理器    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];    //设置请求的数据格式(默认为二进制)    manager.requestSerializer = [AFHTTPRequestSerializer serializer];    //设置响应的数据格式(默认是JSON)    manager.responseSerializer = [AFHTTPResponseSerializer  serializer];        //网络请求    NSString *urlString = @"http://h.hiphotos.baidu.com/image/pic/item/8d5494eef01f3a298a1c0a799c25bc315d607cb5.jpg";        //加载 图片    [self.imageView setImageWithURL:[NSURL URLWithString:urlString]];            AFHTTPRequestOperation *ope = [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {        NSLog(@"成功");        NSLog(@"%@",responseObject);    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {        NSLog(@"失败");    }];            //监控下载进度    [ope setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {        CGFloat progress = totalBytesRead;        NSLog(@"下载进度 = %.2f",progress);    }];                                                    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end





0 0
原创粉丝点击