项目里网络请求是必不可少的,框架使用的是AFNetworking三方库。在其基础上做了一层封装,你使用到的是JMRequestManager,这是一个单例。其主要三个方法GET、POST、upload
第一步:在JMHttpUrl.h定义接口的地址,如下:
#define kUrlLogin fPinUrl(@"api/account/login")//登录
第二步:就是调用接口啦
post请求示例:
NSMutableDictionary *params = [JMCommonMethod baseRequestParams];
[params setJsonValue:phone key:@"mobile"];
[params setJsonValue:code key:@"code"];
[params setJsonValue:password key:@"password"];
[self showLoading];
[[JMRequestManager sharedManager] POST: kUrlLogin parameters:params completion:^(JMBaseResponse *response) {
[self dismissLoading];
if(response.error){
//失败,response.errorMsg就是失败提示语
[JMProgressHelper toastInWindowWithMessage:response.errorMsg];
}else{
//成功,response.responseObject就是接口返回的字典
}
}];
upload请求示例:
- 图片
NSMutableDictionary *params = [JMCommonMethod baseRequestParams];
[params setJsonValue:self.jubaoId key:@"byId"];
NSString *text = self.inputTextView.text;
[params setJsonValue:text key:@"content"];
NSString *contact = self.inputContactTextField.text;
[params setJsonValue:contact key:@"contact"];
[self showLoading];
[[JMRequestManager sharedManager] upload:kUrlJubao parameters:params formDataBlock:^NSDictionary<NSData *,JMDataName *> *(id<AFMultipartFormData> formData, NSMutableDictionary<NSData *,JMDataName *> *needFillDataDict) {
for(int i=0;i<self.pictureData.count;i++){
UIImage *image = self.pictureData[i];
needFillDataDict[UIImageJPEGRepresentation(image, 0.3)] = [NSString stringWithFormat:@"image%d_image.jpg",(i+1)];
}
return needFillDataDict;
} progress:nil completion:^(JMBaseResponse *response) {
[self dismissLoading];
if(response.error){
[JMProgressHelper toastInWindowWithMessage:response.errorMsg];
}else{
[JMProgressHelper toastInWindowWithMessage:response.responseObject[@"desc"]];
[self.navigationController popViewControllerAnimated:YES];
}
}];
注意
needFillDataDict[UIImageJPEGRepresentation(image, 0.3)] = [NSString stringWithFormat:@"image%d_image.jpg",(i+1)];
这个地方的image%d_image.jpg
前面是接口的key,后面是这张图片的名称。
- 视频
NSMutableDictionary *params = [JMCommonMethod baseRequestParams];
[params setJsonValue:[NSString stringWithFormat:@"%d",price.intValue] key:@"price"];
[self showLoading];
//视频压缩
JMUploadVideoTool *videoTool = [[JMUploadVideoTool alloc] init];
[videoTool compressVideoWithInputURL:self.videoUrl successBlock:^(NSURL *outUrl) {
//压缩成功
[[JMRequestManager sharedManager] upload:kUrlUpMai parameters:params formDataBlock:^NSDictionary<NSData *,JMDataName *> *(id<AFMultipartFormData> formData, NSMutableDictionary<NSData *,JMDataName *> *needFillDataDict) {
needFillDataDict[UIImageJPEGRepresentation(self.selectImage, 0.3)] = @"img_image.jpg";
NSData *videoData = [NSData dataWithContentsOfURL:outUrl];
needFillDataDict[videoData] = @"video_video.mp4";
return needFillDataDict;
} progress:nil completion:^(JMBaseResponse *response) {
[self dismissLoading];
if(response.error){
[JMProgressHelper toastInWindowWithMessage:response.errorMsg];
}else{
//移除本地视频
[JMFileManagerHelper removeItemAtPath:[outUrl path]];
[JMFileManagerHelper removeItemAtPath:[self.videoUrl path]];
[UIAlertController jm_showAlertWithTitle:@"上麦成功" message:@"若有用户请求上麦时,您将会接到通知" appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
alertMaker.addActionCancelTitle(@"确定");
} actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
[self.navigationController popViewControllerAnimated:YES];
}];
}
}];
} failBlock:^(NSString *errorMsg) {
//压缩失败
[self dismissLoading];
[JMProgressHelper toastInWindowWithMessage:errorMsg];
}];
这里结合了视频压缩工具一起使用,且是图片+视频一起上传。
结语:讲解的比较佛系,大家不懂的地方直接问我。文章上可以优化的地方,大家也多提建议。
网友评论