==================================================================================================================
typedef void(^TodoSome)(void);
@interface Tools : NSObject
+ (instancetype)toolManager;
+ (void)checkNetWorkWithViewController:(UIViewController *)VC andMessage:(NSString *)message;
+ (void)alertWithMessage:(NSString *)message who:(UIViewController *)vc;
+ (void)alertWithMessage:(NSString *)message andTitle:(NSString *)title who:(UIViewController *)vc;
/**
* 检查网络状态
*/
+ (void)startNetwork;
+ (void)stopNetwork;
+ (void)checkNetWork:(void (^)(BOOL,NSInteger))netStatus;
/**
* 双按钮,取消和确定,提示框
*/
+ (void)toolAlertWithTitle:(NSString *)title andMessage:(NSString *)message byController:(UIViewController *)controller sureBlock:(void (^)(void))sureBlock cancleBlock:(void(^)(void))cancleBlock;
/**
* 单按钮,确定,提示框
*/
+ (void)toolAlertWithTitle:(NSString *)title andMessage:(NSString *)message byController:(UIViewController *)controller sureBlock:(void (^)(void))sureBlock ;
/**
* 双按钮,按钮文自定义,提示框
*/
+ (void)toolAlertWithTitle:(NSString *)title andMessage:(NSString *)message andSureTitle:(NSString *)sure andCancleTitle:(NSString *)cancle byController:(UIViewController *)controller sureBlock:(void (^)(void))sureBlock cancleBlock:(void(^)(void))cancleBlock;
/**
* 单按钮,按钮文自定义,提示框
*/
+ (void)toolAlertWithTitle:(NSString *)title andMessage:(NSString *)message andSureTitle:(NSString *)sure byController:(UIViewController *)controller sureBlock:(void (^)(void))sureBlock;
/**
* 多按钮底部文自定义,提示框
*/
+ (void)toolAlertWithTitleArray:(NSArray *)titles doSomething:(NSArray<TodoSome> *)doSomethings byController:(UIViewController *)controller;
==================================================================================================================
#import "Tools.h"
#import "AFNetworking.h"
@interface Tools ()<UIAlertViewDelegate,UIActionSheetDelegate>
@property (nonatomic, strong) NSMutableArray <TodoSome> *doSomething;
@end
@implementation Tools
//单例模式
static id _instace;
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [super allocWithZone:zone];
});
return _instace;
}
+ (instancetype)toolManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [[self alloc] init];
});
return _instace;
}
#pragma mark 网络检测
+ (void)startNetwork {
AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
// 开始检测测
[netManager startMonitoring];
}
+ (void)stopNetwork {
AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
// 开始检测测
[netManager stopMonitoring];
}
+ (void)checkNetWork:(void (^)(BOOL,NSInteger))netStatus {
// 创建网络检测管理
AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
// 开始检测测
[netManager startMonitoring];
// 检测环节
[netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if (netStatus) {
BOOL isHave = (status != 0);
netStatus(isHave,status);
}
}];
}
+ (void)checkNetWorkWithViewController:(UIViewController *)VC andMessage:(NSString *)message{
// 创建网络检测管理
AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
// 开始检测测
[netManager startMonitoring];
// 检测环节
[netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
// 判断网络状态
switch (status) {
// AFNetworkReachabilityStatusUnknown = -1
case -1:
// [self alertWithMessage:message who:VC];
// MSLog(@"当前网络为未知网络!");
break;
// AFNetworkReachabilityStatusNotReachable = 0
case 0:
[self alertWithMessage:message who:VC];
// MSLog(@"当前状态无网络服务,请检查您的网络!");
break;
// AFNetworkReachabilityStatusReachableViaWWAN = 1
case 1:
[self alertWithMessage:@"当前使用网络为手机数据流量!" who:VC];
// MSLog(@"当前使用网络为手机数据流量!");
break;
// AFNetworkReachabilityStatusReachableViaWiFi = 2
case 2:
[self alertWithMessage:@"当前使用网络为WiFi!" who:VC];
// MSLog(@"当前使用网络为WiFi!");
break;
default:
break;
}
}];
}
#pragma mark -
#pragma mark ==============UIAlertViewDelegate==============
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
Tools *tool = [Tools toolManager];
TodoSome doSomething = tool.doSomething[buttonIndex];
if (doSomething) {
doSomething();
}
}
#pragma mark -
#pragma mark ==============UIActionSheetDelegate==============
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
Tools *tool = [Tools toolManager];
TodoSome doSomething = tool.doSomething[buttonIndex - 1];
if (doSomething) {
doSomething();
}
}
#pragma mark 弹出提示视图
+ (void)toolAlertWithTitleArray:(NSArray *)titles doSomething:(NSArray<TodoSome> *)doSomethings byController:(UIViewController *)controller{
if (kSystemVersion >= 8.0) {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
for (NSInteger index = 0; index < titles.count; index++) {
UIAlertAction *action = [UIAlertAction actionWithTitle:titles[index] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
TodoSome todoSomething = doSomethings[index];
todoSomething();
}];
[alertC addAction:action];
}
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertC addAction:action];
[controller presentViewController:alertC animated:YES completion:nil];
}else {
Tools *tool = [Tools toolManager];
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:tool cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: nil];
for (NSInteger index = 0; index < titles.count; index++) {
[sheet addButtonWithTitle:titles[index]];
tool.doSomething = [doSomethings mutableCopy];
}
[sheet showInView:controller.view];
}
}
+ (void)alertWithMessage:(NSString *)message who:(UIViewController *)vc{
[self alertWithMessage:message andTitle:@"温馨提示" who:vc];
}
+ (void)alertWithMessage:(NSString *)message andTitle:(NSString *)title who:(UIViewController *)vc {
if (kSystemVersion >= 8.0) {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:0 handler:^(UIAlertAction *action) {
} ]];
[vc presentViewController:alertC animated:YES completion:nil];
}else {
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertV show];
}
}
+ (void)toolAlertWithTitle:(NSString *)title andMessage:(NSString *)message byController:(UIViewController *)controller sureBlock:(void (^)(void))sureBlock cancleBlock:(void(^)(void))cancleBlock {
[Tools toolAlertWithTitle:title andMessage:message andSureTitle:@"确定" andCancleTitle:@"取消" byController:controller sureBlock:^{
if (sureBlock) {
sureBlock();
}
} cancleBlock:^{
if (cancleBlock) {
cancleBlock();
}
}];
}
+ (void)toolAlertWithTitle:(NSString *)title andMessage:(NSString *)message andSureTitle:(NSString *)sure andCancleTitle:(NSString *)cancle byController:(UIViewController *)controller sureBlock:(void (^)(void))sureBlock cancleBlock:(void(^)(void))cancleBlock {
if (kSystemVersion >= 8.0) {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alertC addAction:[UIAlertAction actionWithTitle:sure style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if (sureBlock) {
sureBlock();
}
} ]];
[alertC addAction:[UIAlertAction actionWithTitle:cancle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
if (cancleBlock) {
cancleBlock();
}
} ]];
[controller presentViewController:alertC animated:YES completion:nil];
}else {
Tools *tool = [Tools toolManager];
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:title message:message delegate:tool cancelButtonTitle:cancle otherButtonTitles:sure, nil];
TodoSome tod1 = ^(void){
if (sureBlock) {
sureBlock();
}
};
TodoSome tod2 = ^(void){
if (cancleBlock) {
cancleBlock();
}
};
tool.doSomething = [NSMutableArray arrayWithObjects:tod2, tod1, nil];
[alertV show];
}
}
+ (void)toolAlertWithTitle:(NSString *)title andMessage:(NSString *)message byController:(UIViewController *)controller sureBlock:(void (^)(void))sureBlock {
[Tools toolAlertWithTitle:title andMessage:message andSureTitle:@"确定" byController:controller sureBlock:^{
if (sureBlock) {
sureBlock();
}
}];
}
+ (void)toolAlertWithTitle:(NSString *)title andMessage:(NSString *)message andSureTitle:(NSString *)sure byController:(UIViewController *)controller sureBlock:(void (^)(void))sureBlock {
if (kSystemVersion >= 8.0) {
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alertC addAction:[UIAlertAction actionWithTitle:sure style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if (sureBlock) {
sureBlock();
}
} ]];
[controller presentViewController:alertC animated:YES completion:nil];
}else {
Tools *tool = [Tools toolManager];
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:title message:message delegate:tool cancelButtonTitle:nil otherButtonTitles:sure, nil];
TodoSome tod1 = ^(void){
if (sureBlock) {
sureBlock();
}
};
tool.doSomething = [NSMutableArray arrayWithObjects:tod1, nil];
[alertV show];
}
}
@end
使用样例:
[Tools toolAlertWithTitle:@"温馨提示" andMessage:@"message!" andSureTitle:@"确定" andCancleTitle:@"取消" byController:self sureBlock:^{
//do something
} cancleBlock:^{
//do something
}
];