前言
由于独立开发不少
App
,每次新接手一个项目,框架就需要重新搭建。然后开始了漫长的代码复制粘贴。说实在的每次时间浪费在这里是不能容忍的,然后就有了这个项目框架,项目已经放到GitHub
上,欢迎广大网友进行拍砖(轻拍)或者有什么好的设计请Issues
GitHub地址:https://github.com/wanghui9309/BaselineProject
项目介绍
AppDelegate
控制器的切换封装到了
UIWindow+Extension
分类里面,这样代码看起来很简洁,逻辑清楚
//状态栏样式
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
// 初始化SDK
[UIWindow initSDK];
// 1、创建窗口
self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
// 3、显示窗口
[self.window makeKeyAndVisible];
// 2、设置根控制器
[UIWindow switchRootViewController];
Category
Network
对
AFN
简单的二次封装,这个有个遗留问题,每次取消网络请求都会取消全部,暂时不解决
/**
发起请求
@param requestType 请求类型
@param urlStr url
@param photos 图片数组
@param parameters 参数
@param success 请求成功的回调
@param failure 请求失败的回调
*/
- (void)requestWithNetworkType:(NetworkRequestType)requestType
withUrl:(NSString *)urlStr
photos:(NSMutableArray<UIImage *> *)photos
withParameters:(NSDictionary *)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure
{
BOOL isReachable = [[Reachability reachabilityForInternetConnection] isReachable];
if (!isReachable)
{
if (failure)
{
dispatch_async(dispatch_get_main_queue(), ^{
failure(nil);
});
}
return;
}
[[WHNetworkActivityIndicator sharedActivityIndicator] startActivity];
__weak typeof(self) weakSelf = self;
//请求成功的回调
void (^successBlock)(NSURLSessionTask *task, id responseObject) = ^(NSURLSessionTask *task, id responseObject){
[weakSelf.operationTask removeObject:task];
[[WHNetworkActivityIndicator sharedActivityIndicator] stopActivity];
WHLog(@"%@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
if (success)
{
dispatch_async(dispatch_get_main_queue(), ^{
success(responseObject);
});
}
};
//请求失败的回调
void (^failureBlock)(NSURLSessionTask *task, NSError *error) = ^(NSURLSessionTask *task, NSError *error){
[weakSelf.operationTask removeObject:task];
[[WHNetworkActivityIndicator sharedActivityIndicator] stopActivity];
WHLog(@"%@", error);
if (failure)
{
dispatch_async(dispatch_get_main_queue(), ^{
failure(error);
});
}
};
//请求头
switch (self.requestType)
{
case RequestSerializerType_Http:
_sessionManager.requestSerializer = [AFHTTPRequestSerializer serializer];
break;
default:
_sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
break;
}
// 超时时间
_sessionManager.requestSerializer.timeoutInterval = 15.0f;
NSURLSessionTask *task = nil;
switch (requestType)
{
case NetworkRequestType_GET:
{
task = [_sessionManager GET:urlStr parameters:parameters progress:nil success:successBlock failure:failureBlock];
}
break;
case NetworkRequestType_POST:
{
task = [_sessionManager POST:urlStr parameters:parameters progress:nil success:successBlock failure:failureBlock];
}
break;
case NetworkRequestType_FormData:
{
task = [_sessionManager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 直接以 key value 的形式向 formData 中追加二进制数据
for (NSString *key in parameters.allKeys)
{
id data = parameters[key];
if ([data isKindOfClass:NSString.class])
{
[formData appendPartWithFormData:[data dataUsingEncoding:NSUTF8StringEncoding] name:key];
}
}
[photos enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:UIImage.class])
{
NSString *fileName = [NSString stringWithFormat:@"file_%lu.png", idx];
NSData *imageData = UIImageJPEGRepresentation(obj, 0.7);
[formData appendPartWithFileData:imageData name: @"file[]" fileName: fileName mimeType:@"image/png"];
}
}];
} progress:nil success:successBlock failure:failureBlock];
}
break;
default:
break;
}
[_operationTask addObject:task];
}