关于使用YTKNetwork的一点小笔记

1.上传单张或多张图片:

- (AFConstructingBlock)constructingBodyBlock {

return ^(id formData) {

for (unsigned int i = 0; i < self.images.count; i++) {

UIImage *image = [self.images objectAtIndex:i];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

formatter.dateFormat = @"yyyyMMddHHmmss";

NSString *str = [[formatter stringFromDate:[NSDate date]] stringByAppendingString:[NSString stringWithFormat:@"%d",i]];

NSString *fileName;

NSData *data;

NSString *mimeTypeStr;

if (UIImagePNGRepresentation(image) == nil) {

data = UIImageJPEGRepresentation(image, 1.0);

fileName = [NSString stringWithFormat:@"%@.jpg", str];

mimeTypeStr = @"image/jpg";

} else {

data = UIImagePNGRepresentation(image);

fileName = [NSString stringWithFormat:@"%@.png", str];

mimeTypeStr = @"image/png";

}

[formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:mimeTypeStr];

}

};

}

2.Https请求,带证书形式:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];

config.baseUrl = @"https://xxx.com";

//Https证书

NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"cer"];

NSData *certData =[NSData dataWithContentsOfFile:cerPath];

// 验证模式,使用带证书访问模式

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];

// 是否允许自建证书,默认是NO

//securityPolicy.allowInvalidCertificates = YES;

// 是否校验域名信息

securityPolicy.validatesDomainName = YES;

// 添加证书

securityPolicy.pinnedCertificates = [NSSet setWithObject:certData];

config.securityPolicy = securityPolicy;

return YES;

}

你可能感兴趣的:(关于使用YTKNetwork的一点小笔记)