一.UILabel,UIButton, UIImageView ,UIAlertController的封装
新建一个类,.h文件代码如下:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ZlmUI : NSObject
//UILabel
+(UILabel*)createLabelWithFram:(CGRect)cg andFont:(CGFloat)font andTextalignment:(NSInteger)alignment andTextColor:(UIColor *)color andText:(NSString *)text;
//UIImageView
+(UIImageView *)createImageViewWithFram:(CGRect)cg andImageName:(NSString *)name;
//UIButton
+(UIButton *)createButtonWithFram:(CGRect)cg andTarget:(id)target andSelector:(SEL)sel andTitleColor:(UIColor *)color andBackGroundImage:(NSString *)name andTitle:(NSString *)text;
// 弹框提示信息 自定义弹框
+(void)alertWithTitle:(NSString *)title message:(NSString *)message buttonTitle:(NSArray *)buttonTitle confirm:(void(^)(void))ok cancel:(void(^)(void))cancel inController:(UIViewController *)controller;
@end
.m文件代码如下
#import "ZlmUI.h"
@implementation ZlmUI
+(UILabel*)createLabelWithFram:(CGRect)cg andFont:(CGFloat)font andTextalignment:(NSInteger)alignment andTextColor:(UIColor *)color andText:(NSString *)text{
UILabel *lable=[[UILabel alloc]init];
lable.frame=cg;
lable.font=[UIFont systemFontOfSize:font];
lable.userInteractionEnabled=YES;
lable.textAlignment=alignment;
lable.textColor=color;
lable.text=text;
return lable;
}
+(UIImageView *)createImageViewWithFram:(CGRect)cg andImageName:(NSString *)name{
UIImageView *imageV=[[UIImageView alloc]initWithFrame:cg];
imageV.userInteractionEnabled=YES;
imageV.image=[UIImage imageNamed:name];
return imageV;
}
+(UIButton *)createButtonWithFram:(CGRect)cg andTarget:(id)target andSelector:(SEL)sel andTitleColor:(UIColor *)color andBackGroundImage:(NSString *)name andTitle:(NSString *)text{
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
[bt addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
[bt setTitle:text forState:UIControlStateNormal];
[bt setTitleColor:color forState:UIControlStateNormal];
[bt setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
bt.frame=cg;
return bt;
}
+(void)alertWithTitle:(NSString *)title message:(NSString *)message buttonTitle:(NSArray *)buttonTitle confirm:(void (^)(void))ok cancel:(void (^)(void))cancel inController:(UIViewController *)controller{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
NSString *str = buttonTitle[0];
UIAlertAction *action = [UIAlertAction actionWithTitle:str style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (ok) {
ok();//处理
}
}];
[alertController addAction:action];
str = buttonTitle[1];
action = [UIAlertAction actionWithTitle:str style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (cancel) {
cancel();//处理
}
}];
[alertController addAction:action];
[controller presentViewController:alertController animated:YES completion:nil];
}
@end
二.OC AFNetworking3.0的封装
新建一个类,.h文件代码如下:
#import <Foundation/Foundation.h>
#import "MJExtension.h"
#import "NetworkConstant.h"
typedef NS_ENUM(NSInteger, XSRequestType) {
XSRequestTypeForm = 0,
XSRequestTypeJSON = 1
};
typedef void (^SuccessAPIBlock)(id responseObject);//请求成功回调
typedef void (^FailureAPIBlock)(NSError *error);//请求失败回调
typedef void (^FormDataBlock)(id AFMultipartFormData);
@interface XSAPIManager : NSObject
+ (instancetype)manager;
// GET方法请求
- (void)GET:(NSString *)URLString parameters:(id)parameters success:(SuccessAPIBlock)success failure:(FailureAPIBlock)failure;
// POST方法请求
- (void)POST:(NSString *)URLString requestType:(XSRequestType)type parameters:(id)parameters success:(SuccessAPIBlock)success failure:(FailureAPIBlock)failure;
// DELETE
- (void)DELETE:(NSString *)URLString parameters:(id)parameters success:(SuccessAPIBlock)success failure:(FailureAPIBlock)failure;
//上传图片
- (void)MemberPOST:(NSString *)URLString requestType:(XSRequestType)type parameters:(id)parameters constructingBodyWithBlock:(FormDataBlock)block success:(SuccessAPIBlock)success failure:(FailureAPIBlock)failure;
@end
.m文件代码如下
#import "XSAPIManager.h"
#import "AFNetworking.h"
static XSAPIManager * _instance = nil;
@interface XSAPIManager ()
@property (strong, nonatomic, readonly) AFHTTPSessionManager *httpRequestManager;
@end
@implementation XSAPIManager
@synthesize httpRequestManager = _httpRequestManager;
#pragma mark - init
+ (instancetype)manager {
return [[self alloc] init];
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
return _instance;
}
#pragma mark - public methods
- (void)GET:(NSString *)URLString
parameters:(id)parameters
success:(SuccessAPIBlock)success
failure:(FailureAPIBlock)failure {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSLog(@"URLString = %@",URLString);
NSLog(@"参数 = %@",parameters);
[self.httpRequestManager GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
success(responseObject);
NSLog(@"responseObject = %@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(@"Error: %@", error);
if(failure) {
failure(error);
}
} ];
}
- (void)POST:(NSString *)URLString requestType:(XSRequestType)type parameters:(id)parameters success:(SuccessAPIBlock)success failure:(FailureAPIBlock)failure {
NSLog(@"URLString = %@",URLString);
NSLog(@"参数 = %@",parameters);
if (type == XSRequestTypeJSON) {
self.httpRequestManager.requestSerializer = [AFJSONRequestSerializer serializer];
} else if (type == XSRequestTypeForm) {
self.httpRequestManager.requestSerializer = [AFHTTPRequestSerializer serializer];
}
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self.httpRequestManager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject = %@",responseObject);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(@"Error: %@", error);
if(failure) {
failure(error);
}
} ];
}
- (void)DELETE:(NSString *)URLString parameters:(id)parameters success:(SuccessAPIBlock)success failure:(FailureAPIBlock)failure {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self.httpRequestManager DELETE:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(@"Error: %@", error);
if(failure) {
failure(error);
}
}];
}
//图片上传
- (void)MemberPOST:(NSString *)URLString requestType:(XSRequestType)type parameters:(id)parameters constructingBodyWithBlock:(FormDataBlock)block success:(SuccessAPIBlock)success failure:(FailureAPIBlock)failure{
NSLog(@"URLString = %@",URLString);
NSLog(@"参数 = %@",parameters);
if (type == XSRequestTypeJSON) {
self.httpRequestManager.requestSerializer = [AFJSONRequestSerializer serializer];
} else if (type == XSRequestTypeForm) {
self.httpRequestManager.requestSerializer = [AFHTTPRequestSerializer serializer];
}
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self.httpRequestManager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
block(formData);
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject = %@",responseObject);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(@"Error: %@", error);
if(failure) {
failure(error);
}
}];
}
#pragma mark - getters and setters
- (AFHTTPSessionManager *)httpRequestManager {
if (!_httpRequestManager) {
_httpRequestManager = [AFHTTPSessionManager manager];
[_httpRequestManager.requestSerializer setValue:@"utf-8" forHTTPHeaderField:@"charset"];
_httpRequestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/plain", nil];
}
return _httpRequestManager;
}
@end