1. 进入终端,cd 切换至项目文件夹下,执行 ls 指令能够看到 .xcodeproj 文件
2. 创建默认的 Podfile 文件
pod init
3. 搜索 AFNetworking,SDWebImage 框架,得到框架详情(或者直接上 github 上找)
pod search AFNetworking
pod search SDWebImage
4. 使用 Xcode 打开 Podfile 文件,添加框架,保存并关闭
# Uncomment the next line to define a global platform for your project
# 最低支持的 iOS 版本
# platform :ios, '9.0'
target '测试Pod' do
# Comment the next line if you don't want to use dynamic frameworks
# use_frameworks!
pod 'AFNetworking'
pod 'SDWebImage'
# Pods for 测试Pod
end
5. 安装框架,不更新本地索引,速度快
pod install --no-repo-update
6. 之后升级,添加,删除框架 (供参考)
pod update
7. 第一次使用安装框架(供参考)
pod install
8. 打开项目,点击项目中的带白色图标的 .xcworkspace 文件
9. 调用 AFNetworking,SDWebImage 库(库以下载到项目并且编译通过)
#import "ViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
@interface ViewController ()
@property(nonatomic,weak)UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,20, 300, 200)];
[self.view addSubview:iv];
self.imageView = iv;
NSURL *uil = [NSURL URLWithString:@"https://i0.hdslb.com/bfs/article/ec2f90c04aec710ac655db94fd22c103ef47b7c4.jpg@942w_668h_progressive.webp"];
[self.imageView sd_setImageWithURL:uil];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//设置反序列化数据格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
[manager GET:@"http://www.weather.com.cn/data/sk/101010100.html" parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
@end
10. 最后 info.plist 文件中,得加上 ATS 设置,否则报错
<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>