1. 按照 https://www.jianshu.com/p/aed96e11fe36 生成xcode工程后编译cronet_static_framework target会生成静态库 Static/Cronet.framework,
2. 将Cronet.framework 引入自己的xcode工程,并添加以下依赖
CFNetwork.framework
CoreTelephony.framework
MobileServices.framework
Security.framework
SystemConfiguration.framework
UIKit.framework
libresolv.dylib
3. 在自己的xcode工程中修改 other linker flags 添加 -lstdc++
4. 引入<Cronet/Cronet.h>
代码如下
[Cronet setQuicEnabled:YES];
[Cronet setBrotliEnabled:NO];
[Cronet addQuicHint:@"quic.rocks" port:4433 altPort:4433];
[Cronet start];
[Cronet registerHttpProtocolHandler];
https://quic.rocks:4433/ 是用来测试通讯协议的服务
正常发送http请求,cornet会拦截[NSURLSession sharedSession]的请求
- (void)sendQuicRequest{
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask* task = [session dataTaskWithURL:[NSURL URLWithString:@"https://quic.rocks:4433/"] completionHandler:^(NSData * _Nullable jsonData, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString* str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(str);
}];
[task resume];
}
5. Cronet.framework 在app中的实际size为5.5M,对许多app来说太大,需要精简
网友评论