#import <Foundation/Foundation.h>
#import <AFNetworking.h>
#import "YYKit.h"
//http://baobab.kaiyanapp.com/api/v3/video/10774/detail/related?_s=f0e6ee21d170534421fee88afa003612&f=iphone&net=wifi&p_product=EYEPETIZER_IOS&u=aaad82956518235e6e5a34ed0afee16c891ebc4c&v=2.9.0&vc=1604
//http://baobab.kaiyanapp.com/api/v3/video/11226/detail/related?_s=04ada0bbfd44dd2ffb79144735a66891&f=iphone&net=wifi&p_product=EYEPETIZER_IOS&u=aaad82956518235e6e5a34ed0afee16c891ebc4c&v=2.9.0&vc=1604
//http://baobab.kaiyanapp.com/api/v3/discovery?_s=3008ec88188b38ee102de34e3f2c5175&f=iphone&net=wifi&p_product=EYEPETIZER_IOS&u=aaad82956518235e6e5a34ed0afee16c891ebc4c&v=2.9.0&vc=1604
//http://baobab.kaiyanapp.com/api/v3/categories/detail?_s=5252788ace69af5527ed72ce3302fa39&f=iphone&net=wifi&p_product=EYEPETIZER_IOS&u=aaad82956518235e6e5a34ed0afee16c891ebc4c&v=2.9.0&vc=1604
// 获取最新视频
#define mainURL @"http://baobab.kaiyanapp.com/api/v3/tabs/selected?_s=a80176e2f85bce56e8737613ff0686fa&f=iphone&net=wifi&p_product=EYEPETIZER_IOS&u=f36990f193fd8fcefb66969d2ba6043eae73bb9a&v=2.9.0&vc=1604"
// 获取往期视频
#define nextPage @"http://baobab.kaiyanapp.com/api/v3/tabs/selected?pagination=1&needFilter=true&_s=5c08c97c7aed728a12c6792a7a0bbceb&f=iphone&net=wifi&p_product=EYEPETIZER_IOS&u=f36990f193fd8fcefb66969d2ba6043eae73bb9a&v=2.9.0&vc=1604"
// 获取Launch载入图
#define startPage @"http://baobab.wandoujia.com/api/v1/configs?model=iPhone%206%20Plus&version=362&vc=1604&t=MjAxNjExMjYxNjUzMzMzMDQsNzQ5Ng%3D%3D&u=f36990f193fd8fcefb66969d2ba6043eae73bb9a&net=wifi&v=2.9.0&f=iphone"
// 获取作者详情页面数据_s=id
#define authorDetails @"http://baobab.kaiyanapp.com/api/v3/video/10458/detail/related?&f=iphone&net=wifi&p_product=EYEPETIZER_IOS&u=aaad82956518235e6e5a34ed0afee16c891ebc4c&v=2.9.0&vc=1604"
#define findUrl @"http://baobab.kaiyanapp.com/api/v3/discovery?_s=3008ec88188b38ee102de34e3f2c5175&f=iphone&net=wifi&p_product=EYEPETIZER_IOS&u=aaad82956518235e6e5a34ed0afee16c891ebc4c&v=2.9.0&vc=1604"
#define findDetailsUrl @"http://baobab.kaiyanapp.com/api/v3/categories/detail?_s=5252788ace69af5527ed72ce3302fa39&f=iphone&net=wifi&p_product=EYEPETIZER_IOS&u=aaad82956518235e6e5a34ed0afee16c891ebc4c&v=2.9.0&vc=1604"
typedef enum : NSUInteger {
NetworkRequestMethodPOST,
NetworkRequestMethodGET ,
} NetworkRequestMethodENUM;
typedef void (^Success)(_Nonnull id json);
typedef void (^Failure)(NSError *_Nonnull error);
typedef NS_ENUM(NSUInteger, NetworkStates) {
NetworkStatesNone, // 没有网络
NetworkStates2G, // 2G
NetworkStates3G, // 3G
NetworkStates4G, // 4G
NetworkStatesWIFI // WIFI
};
@interface NetworkRequestManage : NSObject
@property (nonnull, nonatomic, strong) AFHTTPSessionManager *requestManager;
// 判断网络类型
+ (NetworkStates)getNetworkStates;
+ (NetworkRequestManage *_Nonnull)sharedManager;
+(void)requestWithRequestMethod:(NetworkRequestMethodENUM)requestMethod
Params:( NSDictionary * _Nonnull )params
Path:(NSString * _Nonnull)path
Success:(_Nonnull Success)success
Failure:(_Nonnull Failure)failure;
@end
#import "NetworkRequestManage.h"
static NetworkRequestManage *_sharedNetworkRequestManage = nil;
@implementation NetworkRequestManage
+ (NetworkRequestManage *)sharedManager {
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
_sharedNetworkRequestManage = [[self alloc] init];
});
return _sharedNetworkRequestManage;
}
- (instancetype) init
{
if (!self) {
self = [super init];
}
if (!self.requestManager) {
self.requestManager = [[AFHTTPSessionManager alloc] init];
// 设置超时时间
self.requestManager.requestSerializer.timeoutInterval = 20.f;
self.requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
}
return self;
}
//基本请求
+(void)requestWithRequestMethod:(NetworkRequestMethodENUM)requestMethod Params:(NSDictionary *)params Path:(NSString *)path Success:(Success)success Failure:(Failure)failure
{
NSString *baseURL = mainURL;
if (path != nil) {
if (path.length != 0) {
baseURL = path;
}
}
NSLog(@"发送请求:%@\n",baseURL);
NSLog(@"发送参数:%@\n",params);
if (requestMethod == NetworkRequestMethodGET) {
[_sharedNetworkRequestManage.requestManager GET:baseURL parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[_sharedNetworkRequestManage processJson:responseObject AtSuccessBlock:success AtFailureBlock:failure];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];
} else {
[_sharedNetworkRequestManage.requestManager POST:baseURL parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[_sharedNetworkRequestManage processJson:responseObject AtSuccessBlock:success AtFailureBlock:failure];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];
}
}
- (void)processJson:(id)json AtSuccessBlock:(Success)success AtFailureBlock:(Failure)failure
{
if (json) {
if ([json isKindOfClass:[NSDictionary class]]||
[json isKindOfClass:[NSArray class]]) {
success(json);
} else {
failure([NSError errorWithDomain:@"服务器错误" code:-1 userInfo:nil]);
}
} else {
failure([NSError errorWithDomain:@"服务器错误" code:-1 userInfo:nil]);
}
}
// 判断网络类型
+ (NetworkStates)getNetworkStates
{
NSArray *subviews = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
// 保存网络状态
NetworkStates states = NetworkStatesNone;
for (id child in subviews) {
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
//获取到状态栏码
int networkType = [[child valueForKeyPath:@"dataNetworkType"] intValue];
switch (networkType) {
case 0:
states = NetworkStatesNone;
//无网模式
break;
case 1:
states = NetworkStates2G;
break;
case 2:
states = NetworkStates3G;
break;
case 3:
states = NetworkStates4G;
break;
case 5:
{
states = NetworkStatesWIFI;
}
break;
default:
break;
}
}
}
//根据状态选择
return states;
}
@end