本文档介绍了MAC iOS SDK的使用方式。
集成前可参考: 移动加速 iOS Demo。
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/aliyun/aliyun-specs.git'
pod 'AlicloudMAC', '~> 1.0.0'
移动加速SDK内部Log查看Tips:可通过过滤字段[MAC查看。
AlicloudMACService *service = [AlicloudMACService sharedInstance];
[service initWithAppKey:@"******" appSecret:@"******" callback:^(BOOL res, NSError *error) {
if (res) {
NSLog(@"MAC SDK init success.");
} else {
NSLog(@"MAC SDK init failed, error: %@", error);
}
}];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.protocolClasses = @[ [MACURLProtocol class] ];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
[[AlicloudMACService sharedInstance] initWithAppKey:testAppKey appSecret:testAppSecret callback:^(BOOL res, NSError *error) {
if (res) {
/* HookURLProtocol注册在SDK初始化之后,因此HookURLProtocol先拦截到网络请求 */
[NSURLProtocol registerClass:[HookURLProtocol class]];
}
}
/**
停止移动加速
*/
- (void)stop:(MACCallbackHandler)callback;
/**
重启移动加速
*/
- (void)restart:(MACCallbackHandler)callback;
[MACACCSNetworkRequest]-[I]: [https://xxx.xxx.com/xx] request result: [1], accelerate result: [1]
/**
降级策略定义
*/
@protocol MACDegradationDelegate <NSObject>
- (BOOL)shouldDegrade:(NSString *)hostName;
@end
/**
SDK回调Handler定义
@param res 回调结果
*/
typedef void (^MACCallbackHandler)(CallbackResult *res);
/**
SDK初始化并开启移动加速
@param appKey AppKey
@param appSecret AppSecret
@param callback 回调
*/
- (void)initWithAppKey:(NSString *)appKey
appSecret:(NSString *)appSecret
callback:(MACCallbackHandler)callback;
/**
设置自定义降级策略
@param delegate 降级策略
*/
- (void)setDegradationPolicy:(id<MACDegradationDelegate>)delegate;
/**
停止移动加速
*/
- (void)stop:(MACCallbackHandler)callback;
/**
重启移动加速
*/
- (void)restart:(MACCallbackHandler)callback;
/**
日志开关
@param enabled YES: 打开; NO: 关闭(默认)
*/
- (void)setLogEnabled:(BOOL)enabled;
/**
初始化MAC SDK
*/
- (void)initMACSDK {
AlicloudMACService *service = [AlicloudMACService sharedInstance];
[service setDegradationPolicy:(id)self];
[service initWithAppKey:@"******" appSecret:@"******" callback:^(BOOL res, NSError *error) {
if (res) {
NSLog(@"MAC SDK init success.");
} else {
NSLog(@"MAC SDK init failed, error: %@", error);
}
}];
}
/**
自定义降级策略
@param url 请求URL
@return YES: 降级到原生网络库; NO: 不降级
*/
- (BOOL)shouldDegrade:(NSURL *)url {
/* 若请求Host为a.b.com,降级走原生网络库 */
if ([[url host] isEqualToString:@"a.b.com"]) {
return YES;
}
return NO;
}
static NSURLSession *_session;
/**
发网络请求
*/
- (void)sendNetworkReqeust {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
/* 若基于NSURLSession发网络请求并配置SessionConfiguration,需要注册MACURLProtocol */
if (!_session) {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.protocolClasses = @[ [MACURLProtocol class] ];
_session = [NSURLSession sessionWithConfiguration:configuration];
}
});
NSURL *url = [NSURL URLWithString:@"xxxxxx"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *task = [_session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
return;
}
NSLog(@"Content: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
[task resume];
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。