ad localnoti background music
This commit is contained in:
@@ -33,6 +33,11 @@
|
||||
*/
|
||||
+(long)getFutureTimetstamp:(NSUInteger)hour minute:(NSUInteger)min second:(NSUInteger)second;
|
||||
|
||||
/**
|
||||
* 系统当前时间时间戳
|
||||
*/
|
||||
+(long)getNowDateTimestamp;
|
||||
|
||||
/**
|
||||
* NSTimeInterval 转分钟秒
|
||||
*/
|
||||
|
||||
@@ -60,11 +60,27 @@
|
||||
|
||||
}
|
||||
|
||||
+(long)getNowDateTimestamp{
|
||||
|
||||
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
||||
// ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
|
||||
[formatter setDateFormat:@"YYYY年MM月dd日HH:mm:ss"];
|
||||
|
||||
//现在时间,你可以输出来看下是什么格式
|
||||
NSDate *datenow = [NSDate date];
|
||||
//----------将nsdate按formatter格式转成NSString
|
||||
NSString *currentTimeString_1 = [formatter stringFromDate:datenow];
|
||||
NSDate *applyTimeString_1 = [formatter dateFromString:currentTimeString_1];
|
||||
long nowTimeSp = (long)[applyTimeString_1 timeIntervalSince1970];
|
||||
return nowTimeSp;
|
||||
}
|
||||
|
||||
+(NSString *)stringWithNSTimerinterval:(NSTimeInterval)interval{
|
||||
|
||||
NSInteger hour = interval / (60*60);
|
||||
NSInteger min = interval / 60;
|
||||
NSInteger sec = (NSInteger) interval % 60;
|
||||
return [NSString stringWithFormat:@"%02ld:%02ld",min,sec];
|
||||
return [NSString stringWithFormat:@"%02ld:%02ld:%02ld",hour,min,sec];
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// GiGaLocalNotificationManager.h
|
||||
// GIGA
|
||||
//
|
||||
// Created by lianxiang on 2018/8/24.
|
||||
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UserNotifications/UserNotifications.h>
|
||||
|
||||
@interface GiGaLocalNotificationManager : NSObject<UNUserNotificationCenterDelegate>
|
||||
|
||||
/**
|
||||
本地推送管理中心
|
||||
*/
|
||||
+(GiGaLocalNotificationManager*)localNotifiationCenter;
|
||||
|
||||
/**
|
||||
发送本地通知
|
||||
ios 8 后 iOS10 前
|
||||
@param alertBoday 通知显示内容
|
||||
@param timeInterval 设置通知发送时间,单位秒
|
||||
@param alertAction 解锁滑动时事件
|
||||
@param identifier ios 10 即是Identifier iOS8 是userInfo key value 值
|
||||
*/
|
||||
-(void)sendLocalNotification:(NSString *)alertBoday fireTimeInterval:(NSTimeInterval )timeInterval alertAction:(NSString *)alertAction withIdentifier:(NSString *)identifier;
|
||||
|
||||
/**
|
||||
删除当前程序注册的所有通知 ios 8 后 iOS10 前
|
||||
*/
|
||||
-(void)cancelAllLocalNoitification;
|
||||
|
||||
/**
|
||||
删除指定的通知,一般用于取消重复的通知或者还没有被调用的通知,先获取通知,再遍历根据条件去删除(条件是 UserInfo 的值,是发送通知时所携带的参数) ios 8 后 iOS10 前
|
||||
*/
|
||||
-(void)cancelLocalNitificationByUserInfowithIdentifier:(NSString *)identifier;
|
||||
|
||||
/**
|
||||
iOS 8 收到本地通知 iOS10通过 UNUserNotificationCenterDelegate实现
|
||||
*/
|
||||
-(void)didResaveloaclNitification:(UILocalNotification *)localNitification;
|
||||
|
||||
|
||||
//处理通知。。
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,164 @@
|
||||
//
|
||||
// GiGaLocalNotificationManager.m
|
||||
// GIGA
|
||||
//
|
||||
// Created by lianxiang on 2018/8/24.
|
||||
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
||||
//
|
||||
|
||||
#import "GiGaLocalNotificationManager.h"
|
||||
|
||||
@implementation GiGaLocalNotificationManager
|
||||
|
||||
+(GiGaLocalNotificationManager*)localNotifiationCenter{
|
||||
static GiGaLocalNotificationManager *center = nil;
|
||||
static dispatch_once_t once;
|
||||
dispatch_once(&once, ^{
|
||||
center = [[self alloc] init];
|
||||
});
|
||||
return center;
|
||||
}
|
||||
|
||||
-(void)sendLocalNotification:(NSString *)alertBoday fireTimeInterval:(NSTimeInterval )timeInterval alertAction:(NSString *)alertAction withIdentifier:(NSString *)identifier{
|
||||
|
||||
if (@available(iOS 10.0, *)) {
|
||||
|
||||
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
||||
center.delegate = self;
|
||||
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
|
||||
content.title = alertAction;
|
||||
content.body = alertBoday;
|
||||
content.sound = [UNNotificationSound defaultSound];
|
||||
//content.sound = UNNotificationSound
|
||||
//通知附件 音<10M p3 p4 ,视频<50M pmeg mpeg4 图片<5M
|
||||
|
||||
NSURL *imageUrl = [[NSBundle mainBundle] URLForResource:@"MaskTime" withExtension:@"png"];
|
||||
UNNotificationAttachment *attach = [UNNotificationAttachment attachmentWithIdentifier:@"photo" URL:imageUrl options:nil error:nil];
|
||||
|
||||
NSURL *audioUrl = [[NSBundle mainBundle] URLForResource:@"pomodoSound" withExtension:@"m4a"];
|
||||
UNNotificationAttachment *attachAudio = [UNNotificationAttachment attachmentWithIdentifier:@"audio" URL:audioUrl options:nil error:nil];
|
||||
|
||||
NSURL *vedioUrl = [[NSBundle mainBundle] URLForResource:@"emojizone" withExtension:@"mp4"];
|
||||
UNNotificationAttachment *vedioAudio = [UNNotificationAttachment attachmentWithIdentifier:@"vedio" URL:vedioUrl options:nil error:nil];
|
||||
content.attachments = @[attach,attachAudio,vedioAudio];
|
||||
|
||||
|
||||
//延迟通知 第一个参数是重复的时间间隔,最小60s,第二个参数是是否重复。
|
||||
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];
|
||||
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
|
||||
|
||||
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
|
||||
GILog(@"成功添加推送");
|
||||
|
||||
}];
|
||||
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
|
||||
//ios8
|
||||
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
|
||||
localNotification.alertBody = alertBoday;
|
||||
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
|
||||
localNotification.alertAction = alertAction;
|
||||
localNotification.applicationIconBadgeNumber = 1;
|
||||
localNotification.soundName = UILocalNotificationDefaultSoundName;
|
||||
//localNotification.alertLaunchImage = @"MaskTime.png";
|
||||
localNotification.userInfo = @{identifier:identifier};
|
||||
//根据设定时间发送通知
|
||||
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
-(void)cancelAllLocalNoitification
|
||||
{
|
||||
|
||||
if (@available(iOS 10.0, *)) {
|
||||
|
||||
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
|
||||
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
[[UIApplication sharedApplication] cancelAllLocalNotifications];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
-(void)cancelLocalNitificationByUserInfowithIdentifier:(NSString *)identifier{
|
||||
|
||||
if (@available(iOS 10.0, *)) {
|
||||
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[identifier]];
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
NSArray *notifiArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
|
||||
for (UILocalNotification *local in notifiArray) {
|
||||
//将来可以根据UserInfo的值,来查看这个是否是你想要删除的通知
|
||||
if (local.userInfo) {
|
||||
//删除单个通知
|
||||
NSDictionary *useinfo = local.userInfo;
|
||||
NSString *timefalg = useinfo[identifier];
|
||||
if ([timefalg isEqualToString:identifier] ) {
|
||||
[[UIApplication sharedApplication]cancelLocalNotification:local];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//将通知传递给前台运行的app
|
||||
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
|
||||
API_AVAILABLE(ios(10.0)){
|
||||
|
||||
NSDictionary * userInfo = notification.request.content.userInfo;
|
||||
UNNotificationRequest *request = notification.request; // 收到推送的请求
|
||||
UNNotificationContent *content = request.content; // 收到推送的消息内容
|
||||
NSNumber *badge = content.badge; // 推送消息的角标
|
||||
NSString *body = content.body; // 推送消息体
|
||||
UNNotificationSound *sound = content.sound; // 推送消息的声音
|
||||
NSString *subtitle = content.subtitle; // 推送消息的副标题
|
||||
NSString *title = content.title; // 推送消息的标题
|
||||
|
||||
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
|
||||
// 远程推送通知在AppDelegate+ThirdParty 中处理
|
||||
NSLog(@"iOS10 前台 收到远程通知:%@", body);
|
||||
|
||||
} else {
|
||||
// 判断为本地通知
|
||||
NSLog(@"iOS10 前台 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
|
||||
|
||||
[self showAlert:title message:body];
|
||||
}
|
||||
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
|
||||
}
|
||||
|
||||
//将用户对通知响应结果告诉app
|
||||
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
|
||||
API_AVAILABLE(ios(10.0)){
|
||||
|
||||
GILog(@"ios10用户点击通知栏相应");
|
||||
|
||||
completionHandler();
|
||||
}
|
||||
//
|
||||
-(void)didResaveloaclNitification:(UILocalNotification *)localNitification
|
||||
{
|
||||
//NSDictionary * userInfo = localNitification.userInfo;
|
||||
|
||||
NSString *title = localNitification.alertTitle;
|
||||
NSString *message = localNitification.alertBody;
|
||||
[self showAlert:title message:message];
|
||||
GILog(@"用户点击通知栏相应");
|
||||
|
||||
}
|
||||
|
||||
-(void)showAlert:(NSString *)title message:(NSString *)message
|
||||
{
|
||||
[[UIApplication sharedApplication].keyWindow.rootViewController jxt_showAlertWithTitle:title message:message appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
|
||||
alertMaker.addActionCancelTitle(@"知道了");
|
||||
|
||||
} actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
|
||||
|
||||
}];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,138 @@
|
||||
//
|
||||
// JXTAlertController.h
|
||||
// JXTAlertManager
|
||||
//
|
||||
// Created by JXT on 2016/12/22.
|
||||
// Copyright © 2016年 JXT. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
#pragma mark - I.JXTAlertController构造
|
||||
|
||||
@class JXTAlertController;
|
||||
/**
|
||||
JXTAlertController: alertAction配置链
|
||||
|
||||
@param title 标题
|
||||
@return JXTAlertController对象
|
||||
*/
|
||||
typedef JXTAlertController * _Nonnull (^JXTAlertActionTitle)(NSString *title);
|
||||
|
||||
/**
|
||||
JXTAlertController: alert按钮执行回调
|
||||
|
||||
@param buttonIndex 按钮index(根据添加action的顺序)
|
||||
@param action UIAlertAction对象
|
||||
@param alertSelf 本类对象
|
||||
*/
|
||||
typedef void (^JXTAlertActionBlock)(NSInteger buttonIndex, UIAlertAction *action, JXTAlertController *alertSelf);
|
||||
|
||||
|
||||
/**
|
||||
JXTAlertController 简介:
|
||||
|
||||
1.针对系统UIAlertController封装,支持iOS8及以上
|
||||
|
||||
2.关于iOS9之后的`preferredAction`属性用法:
|
||||
`alertController.preferredAction = alertController.actions[0];`
|
||||
效果为将已存在的某个action字体加粗,原cancel样式的加粗字体成为deafult样式,cancel样式的action仍然排列在最下
|
||||
总体意义不大,且仅限于`UIAlertControllerStyleAlert`,actionSheet无效,功能略微鸡肋,不再单独封装
|
||||
|
||||
3.关于`addTextFieldWithConfigurationHandler:`方法:
|
||||
该方法同样仅限于`UIAlertControllerStyleAlert`使用,使用场景较为局限,推荐直接调用,不再针对封装
|
||||
|
||||
4.关于自定义按钮字体或者颜色,可以利用kvc间接访问这些私有属性,但是不推荐
|
||||
`[alertAction setValue:[UIColor grayColor] forKey:@"titleTextColor"]`
|
||||
*/
|
||||
NS_CLASS_AVAILABLE_IOS(8_0) @interface JXTAlertController : UIAlertController
|
||||
|
||||
|
||||
/**
|
||||
JXTAlertController: 禁用alert弹出动画,默认执行系统的默认弹出动画
|
||||
*/
|
||||
- (void)alertAnimateDisabled;
|
||||
|
||||
/**
|
||||
JXTAlertController: alert弹出后,可配置的回调
|
||||
*/
|
||||
@property (nullable, nonatomic, copy) void (^alertDidShown)(void);
|
||||
|
||||
/**
|
||||
JXTAlertController: alert关闭后,可配置的回调
|
||||
*/
|
||||
@property (nullable, nonatomic, copy) void (^alertDidDismiss)(void);
|
||||
|
||||
/**
|
||||
JXTAlertController: 设置toast模式展示时间:如果alert未添加任何按钮,将会以toast样式展示,这里设置展示时间,默认1s
|
||||
*/
|
||||
@property (nonatomic, assign) NSTimeInterval toastStyleDuration; //deafult jxt_alertShowDurationDefault = 1s
|
||||
|
||||
|
||||
/**
|
||||
JXTAlertController: 链式构造alert视图按钮,添加一个alertAction按钮,默认样式,参数为标题
|
||||
|
||||
@return JXTAlertController对象
|
||||
*/
|
||||
- (JXTAlertActionTitle)addActionDefaultTitle;
|
||||
|
||||
/**
|
||||
JXTAlertController: 链式构造alert视图按钮,添加一个alertAction按钮,取消样式,参数为标题(warning:一个alert该样式只能添加一次!!!)
|
||||
|
||||
@return JXTAlertController对象
|
||||
*/
|
||||
- (JXTAlertActionTitle)addActionCancelTitle;
|
||||
|
||||
/**
|
||||
JXTAlertController: 链式构造alert视图按钮,添加一个alertAction按钮,警告样式,参数为标题
|
||||
|
||||
@return JXTAlertController对象
|
||||
*/
|
||||
- (JXTAlertActionTitle)addActionDestructiveTitle;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
#pragma mark - II.UIViewController扩展使用JXTAlertController
|
||||
|
||||
/**
|
||||
JXTAlertController: alert构造块
|
||||
|
||||
@param alertMaker JXTAlertController配置对象
|
||||
*/
|
||||
typedef void(^JXTAlertAppearanceProcess)(JXTAlertController *alertMaker);
|
||||
|
||||
@interface UIViewController (JXTAlertController)
|
||||
|
||||
/**
|
||||
JXTAlertController: show-alert(iOS8)
|
||||
|
||||
@param title title
|
||||
@param message message
|
||||
@param appearanceProcess alert配置过程
|
||||
@param actionBlock alert点击响应回调
|
||||
*/
|
||||
- (void)jxt_showAlertWithTitle:(nullable NSString *)title
|
||||
message:(nullable NSString *)message
|
||||
appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess
|
||||
actionsBlock:(nullable JXTAlertActionBlock)actionBlock NS_AVAILABLE_IOS(8_0);
|
||||
|
||||
/**
|
||||
JXTAlertController: show-actionSheet(iOS8)
|
||||
|
||||
@param title title
|
||||
@param message message
|
||||
@param appearanceProcess actionSheet配置过程
|
||||
@param actionBlock actionSheet点击响应回调
|
||||
*/
|
||||
- (void)jxt_showActionSheetWithTitle:(nullable NSString *)title
|
||||
message:(nullable NSString *)message
|
||||
appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess
|
||||
actionsBlock:(nullable JXTAlertActionBlock)actionBlock NS_AVAILABLE_IOS(8_0);
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,216 @@
|
||||
//
|
||||
// JXTAlertController.m
|
||||
// JXTAlertManager
|
||||
//
|
||||
// Created by JXT on 2016/12/22.
|
||||
// Copyright © 2016年 JXT. All rights reserved.
|
||||
//
|
||||
|
||||
#import "JXTAlertController.h"
|
||||
|
||||
//toast默认展示时间
|
||||
static NSTimeInterval const JXTAlertShowDurationDefault = 1.0f;
|
||||
|
||||
|
||||
#pragma mark - I.AlertActionModel
|
||||
@interface JXTAlertActionModel : NSObject
|
||||
@property (nonatomic, copy) NSString * title;
|
||||
@property (nonatomic, assign) UIAlertActionStyle style;
|
||||
@end
|
||||
@implementation JXTAlertActionModel
|
||||
- (instancetype)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
self.title = @"";
|
||||
self.style = UIAlertActionStyleDefault;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@end
|
||||
|
||||
|
||||
|
||||
#pragma mark - II.JXTAlertController
|
||||
/**
|
||||
AlertActions配置
|
||||
|
||||
@param actionBlock JXTAlertActionBlock
|
||||
*/
|
||||
typedef void (^JXTAlertActionsConfig)(JXTAlertActionBlock actionBlock);
|
||||
|
||||
|
||||
@interface JXTAlertController ()
|
||||
//JXTAlertActionModel数组
|
||||
@property (nonatomic, strong) NSMutableArray <JXTAlertActionModel *>* jxt_alertActionArray;
|
||||
//是否操作动画
|
||||
@property (nonatomic, assign) BOOL jxt_setAlertAnimated;
|
||||
//action配置
|
||||
- (JXTAlertActionsConfig)alertActionsConfig;
|
||||
@end
|
||||
|
||||
@implementation JXTAlertController
|
||||
|
||||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
}
|
||||
- (void)didReceiveMemoryWarning {
|
||||
[super didReceiveMemoryWarning];
|
||||
}
|
||||
- (void)viewDidDisappear:(BOOL)animated
|
||||
{
|
||||
[super viewDidDisappear:animated];
|
||||
if (self.alertDidDismiss) {
|
||||
self.alertDidDismiss();
|
||||
}
|
||||
}
|
||||
- (void)dealloc
|
||||
{
|
||||
// NSLog(@"test-dealloc");
|
||||
}
|
||||
|
||||
#pragma mark - Private
|
||||
//action-title数组
|
||||
- (NSMutableArray<JXTAlertActionModel *> *)jxt_alertActionArray
|
||||
{
|
||||
if (_jxt_alertActionArray == nil) {
|
||||
_jxt_alertActionArray = [NSMutableArray array];
|
||||
}
|
||||
return _jxt_alertActionArray;
|
||||
}
|
||||
//action配置
|
||||
- (JXTAlertActionsConfig)alertActionsConfig
|
||||
{
|
||||
return ^(JXTAlertActionBlock actionBlock) {
|
||||
if (self.jxt_alertActionArray.count > 0)
|
||||
{
|
||||
//创建action
|
||||
__weak typeof(self)weakSelf = self;
|
||||
[self.jxt_alertActionArray enumerateObjectsUsingBlock:^(JXTAlertActionModel *actionModel, NSUInteger idx, BOOL * _Nonnull stop) {
|
||||
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:actionModel.title style:actionModel.style handler:^(UIAlertAction * _Nonnull action) {
|
||||
__strong typeof(weakSelf)strongSelf = weakSelf;
|
||||
if (actionBlock) {
|
||||
actionBlock(idx, action, strongSelf);
|
||||
}
|
||||
}];
|
||||
//可利用这个改变字体颜色,但是不推荐!!!
|
||||
// [alertAction setValue:[UIColor grayColor] forKey:@"titleTextColor"];
|
||||
//action作为self元素,其block实现如果引用本类指针,会造成循环引用
|
||||
[self addAction:alertAction];
|
||||
}];
|
||||
}
|
||||
else
|
||||
{
|
||||
NSTimeInterval duration = self.toastStyleDuration > 0 ? self.toastStyleDuration : JXTAlertShowDurationDefault;
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
[self dismissViewControllerAnimated:!(self.jxt_setAlertAnimated) completion:NULL];
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#pragma mark - Public
|
||||
|
||||
- (instancetype)initAlertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle
|
||||
{
|
||||
if (!(title.length > 0) && (message.length > 0) && (preferredStyle == UIAlertControllerStyleAlert)) {
|
||||
title = @"";
|
||||
}
|
||||
self = [[self class] alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
|
||||
if (!self) return nil;
|
||||
|
||||
self.jxt_setAlertAnimated = NO;
|
||||
self.toastStyleDuration = JXTAlertShowDurationDefault;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)alertAnimateDisabled
|
||||
{
|
||||
self.jxt_setAlertAnimated = YES;
|
||||
}
|
||||
|
||||
- (JXTAlertActionTitle)addActionDefaultTitle
|
||||
{
|
||||
//该block返回值不是本类属性,只是局部变量,不会造成循环引用
|
||||
return ^(NSString *title) {
|
||||
JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
|
||||
actionModel.title = title;
|
||||
actionModel.style = UIAlertActionStyleDefault;
|
||||
[self.jxt_alertActionArray addObject:actionModel];
|
||||
return self;
|
||||
};
|
||||
}
|
||||
|
||||
- (JXTAlertActionTitle)addActionCancelTitle
|
||||
{
|
||||
return ^(NSString *title) {
|
||||
JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
|
||||
actionModel.title = title;
|
||||
actionModel.style = UIAlertActionStyleCancel;
|
||||
[self.jxt_alertActionArray addObject:actionModel];
|
||||
return self;
|
||||
};
|
||||
}
|
||||
|
||||
- (JXTAlertActionTitle)addActionDestructiveTitle
|
||||
{
|
||||
return ^(NSString *title) {
|
||||
JXTAlertActionModel *actionModel = [[JXTAlertActionModel alloc] init];
|
||||
actionModel.title = title;
|
||||
actionModel.style = UIAlertActionStyleDestructive;
|
||||
[self.jxt_alertActionArray addObject:actionModel];
|
||||
return self;
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
|
||||
#pragma mark - III.UIViewController扩展
|
||||
@implementation UIViewController (JXTAlertController)
|
||||
|
||||
- (void)jxt_showAlertWithPreferredStyle:(UIAlertControllerStyle)preferredStyle title:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
|
||||
{
|
||||
if (appearanceProcess)
|
||||
{
|
||||
JXTAlertController *alertMaker = [[JXTAlertController alloc] initAlertControllerWithTitle:title message:message preferredStyle:preferredStyle];
|
||||
//防止nil
|
||||
if (!alertMaker) {
|
||||
return ;
|
||||
}
|
||||
//加工链
|
||||
appearanceProcess(alertMaker);
|
||||
//配置响应
|
||||
alertMaker.alertActionsConfig(actionBlock);
|
||||
// alertMaker.alertActionsConfig(^(NSInteger buttonIndex, UIAlertAction *action){
|
||||
// if (actionBlock) {
|
||||
// actionBlock(buttonIndex, action);
|
||||
// }
|
||||
// });
|
||||
|
||||
if (alertMaker.alertDidShown)
|
||||
{
|
||||
[self presentViewController:alertMaker animated:!(alertMaker.jxt_setAlertAnimated) completion:^{
|
||||
alertMaker.alertDidShown();
|
||||
}];
|
||||
}
|
||||
else
|
||||
{
|
||||
[self presentViewController:alertMaker animated:!(alertMaker.jxt_setAlertAnimated) completion:NULL];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)jxt_showAlertWithTitle:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
|
||||
{
|
||||
[self jxt_showAlertWithPreferredStyle:UIAlertControllerStyleAlert title:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
|
||||
}
|
||||
|
||||
- (void)jxt_showActionSheetWithTitle:(NSString *)title message:(NSString *)message appearanceProcess:(JXTAlertAppearanceProcess)appearanceProcess actionsBlock:(JXTAlertActionBlock)actionBlock
|
||||
{
|
||||
[self jxt_showAlertWithPreferredStyle:UIAlertControllerStyleActionSheet title:title message:message appearanceProcess:appearanceProcess actionsBlock:actionBlock];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
+301
@@ -0,0 +1,301 @@
|
||||
//
|
||||
// JXTAlertView.h
|
||||
// JXTAlertManager
|
||||
//
|
||||
// Created by JXT on 2016/12/20.
|
||||
// Copyright © 2016年 JXT. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
#define jxt_dispatch_main_async_safe(block)\
|
||||
if ([NSThread isMainThread]) {\
|
||||
block();\
|
||||
} else {\
|
||||
dispatch_async(dispatch_get_main_queue(), block);\
|
||||
}
|
||||
/**
|
||||
回调主线程(显示alert必须在主线程执行)
|
||||
|
||||
@param block 执行块
|
||||
*/
|
||||
static inline void jxt_getSafeMainQueue(_Nonnull dispatch_block_t block)
|
||||
{
|
||||
jxt_dispatch_main_async_safe(block);
|
||||
}
|
||||
|
||||
/**
|
||||
alert按钮执行回调
|
||||
|
||||
@param buttonIndex 按钮index
|
||||
*/
|
||||
typedef void (^JXTAlertClickBlock)(NSInteger buttonIndex);
|
||||
|
||||
|
||||
// MARK: 1.常规的alert
|
||||
/**
|
||||
* JXTAlertView: 两个按钮alert
|
||||
*/
|
||||
void jxt_showAlertTwoButton(NSString * _Nullable title,
|
||||
NSString * _Nullable message,
|
||||
NSString * _Nullable cancelButtonTitle,
|
||||
JXTAlertClickBlock _Nullable cancelBlock,
|
||||
NSString * _Nullable otherButtonTitle,
|
||||
JXTAlertClickBlock _Nullable otherBlock);
|
||||
/**
|
||||
* JXTAlertView: 一个按钮alert
|
||||
*/
|
||||
void jxt_showAlertOneButton(NSString * _Nullable title,
|
||||
NSString * _Nullable message,
|
||||
NSString * _Nullable cancelButtonTitle,
|
||||
JXTAlertClickBlock _Nullable cancelBlock);
|
||||
/**
|
||||
* JXTAlertView: 一个固定按钮alert
|
||||
*/
|
||||
void jxt_showAlertTitle(NSString * _Nullable title);
|
||||
/**
|
||||
* JXTAlertView: 一个固定按钮alert
|
||||
*/
|
||||
void jxt_showAlertMessage(NSString * _Nullable message);
|
||||
/**
|
||||
* JXTAlertView: 一个固定按钮alert
|
||||
*/
|
||||
void jxt_showAlertTitleMessage(NSString * _Nullable title,
|
||||
NSString * _Nullable message);
|
||||
|
||||
|
||||
// MARK: 2.无按钮toast
|
||||
/**
|
||||
* JXTAlertView: 无按钮toast,支持自定义关闭回调
|
||||
*/
|
||||
void jxt_showToastTitleMessageDismiss(NSString * _Nullable title,
|
||||
NSString * _Nullable message,
|
||||
NSTimeInterval duration,
|
||||
JXTAlertClickBlock _Nullable dismissCompletion);
|
||||
/**
|
||||
* JXTAlertView: 无按钮toast,支持自定义关闭回调
|
||||
*/
|
||||
void jxt_showToastTitleDismiss(NSString * _Nullable title,
|
||||
NSTimeInterval duration,
|
||||
JXTAlertClickBlock _Nullable dismissCompletion);
|
||||
/**
|
||||
* JXTAlertView: 无按钮toast,支持自定义关闭回调
|
||||
*/
|
||||
void jxt_showToastMessageDismiss(NSString * _Nullable message,
|
||||
NSTimeInterval duration,
|
||||
JXTAlertClickBlock _Nullable dismissCompletion);
|
||||
/**
|
||||
* JXTAlertView: 无按钮toast
|
||||
*/
|
||||
void jxt_showToastTitle(NSString * _Nullable title,
|
||||
NSTimeInterval duration);
|
||||
/**
|
||||
* JXTAlertView: 无按钮toast
|
||||
*/
|
||||
void jxt_showToastMessage(NSString * _Nullable message,
|
||||
NSTimeInterval duration);
|
||||
|
||||
|
||||
// MARK: 3.文字HUD,代码执行关闭
|
||||
/**
|
||||
* JXTAlertView: 文字HUD,jxt_dismissHUD()执行关闭
|
||||
*/
|
||||
void jxt_showTextHUDTitleMessage(NSString * _Nullable title,
|
||||
NSString * _Nullable message);
|
||||
/**
|
||||
* JXTAlertView: 文字HUD,jxt_dismissHUD()执行关闭
|
||||
*/
|
||||
void jxt_showTextHUDTitle(NSString * _Nullable title);
|
||||
/**
|
||||
* JXTAlertView: 文字HUD,jxt_dismissHUD()执行关闭
|
||||
*/
|
||||
void jxt_showTextHUDMessage(NSString * _Nullable message);
|
||||
|
||||
|
||||
// MARK: 4.loadHUD,代码执行关闭
|
||||
/**
|
||||
* JXTAlertView: loadHUD,jxt_dismissHUD()执行关闭
|
||||
*/
|
||||
void jxt_showLoadingHUDTitleMessage(NSString * _Nullable title,
|
||||
NSString * _Nullable message);
|
||||
/**
|
||||
* JXTAlertView: loadHUD,jxt_dismissHUD()执行关闭
|
||||
*/
|
||||
void jxt_showLoadingHUDTitle(NSString * _Nullable title);
|
||||
/**
|
||||
* JXTAlertView: loadHUD,jxt_dismissHUD()执行关闭
|
||||
*/
|
||||
void jxt_showLoadingHUDMessage(NSString * _Nullable message);
|
||||
|
||||
|
||||
// MARK: 5.ProgressHUD,代码执行关闭
|
||||
/**
|
||||
* JXTAlertView: ProgressHUD,jxt_dismissHUD()执行关闭
|
||||
*/
|
||||
void jxt_showProgressHUDTitleMessage(NSString * _Nullable title,
|
||||
NSString * _Nullable message);
|
||||
/**
|
||||
* JXTAlertView: ProgressHUD,jxt_dismissHUD()执行关闭
|
||||
*/
|
||||
void jxt_showProgressHUDTitle(NSString * _Nullable title);
|
||||
/**
|
||||
* JXTAlertView: ProgressHUD,jxt_dismissHUD()执行关闭
|
||||
*/
|
||||
void jxt_showProgressHUDMessage(NSString * _Nullable message);
|
||||
/**
|
||||
* JXTAlertView: ProgressHUD,设置进度值
|
||||
*/
|
||||
void jxt_setHUDProgress(float progress);
|
||||
|
||||
|
||||
// MARK: 6.HUD公用
|
||||
/**
|
||||
* JXTAlertView: 设置HUD成功状态
|
||||
*/
|
||||
void jxt_setHUDSuccessTitleMessage(NSString * _Nullable title,
|
||||
NSString * _Nullable message);
|
||||
/**
|
||||
* JXTAlertView: 设置HUD成功状态
|
||||
*/
|
||||
void jxt_setHUDSuccessTitle(NSString * _Nullable title);
|
||||
/**
|
||||
* JXTAlertView: 设置HUD成功状态
|
||||
*/
|
||||
void jxt_setHUDSuccessMessage(NSString * _Nullable message);
|
||||
|
||||
/**
|
||||
* JXTAlertView: 设置HUD失败状态
|
||||
*/
|
||||
void jxt_setHUDFailTitleMessage(NSString * _Nullable title,
|
||||
NSString * _Nullable message);
|
||||
/**
|
||||
* JXTAlertView: 设置HUD失败状态
|
||||
*/
|
||||
void jxt_setHUDFailTitle(NSString * _Nullable title);
|
||||
/**
|
||||
* JXTAlertView: 设置HUD失败状态
|
||||
*/
|
||||
void jxt_setHUDFailMessage(NSString * _Nullable message);
|
||||
|
||||
/**
|
||||
* JXTAlertView: 关闭HUD
|
||||
*/
|
||||
void jxt_dismissHUD(void);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
JXTAlertView 简介:
|
||||
|
||||
开发调试使用简易alert/HUD工具
|
||||
|
||||
部分提供C函数方便使用,所有show方法的C函数均默认回调主线程
|
||||
*/
|
||||
@interface JXTAlertView : UIAlertView
|
||||
|
||||
/**
|
||||
JXTAlertView: 最多支持两个按钮的alert
|
||||
|
||||
@param title title
|
||||
@param message message
|
||||
@param cancelButtonTitle 取消按钮标题
|
||||
@param otherButtonTitle 其他按钮标题
|
||||
@param cancelBlock 取消按钮回调
|
||||
@param otherBlock 其他按钮回调
|
||||
*/
|
||||
+ (void)showAlertViewWithTitle:(nullable NSString *)title
|
||||
message:(nullable NSString *)message
|
||||
cancelButtonTitle:(nullable NSString *)cancelButtonTitle
|
||||
otherButtonTitle:(nullable NSString *)otherButtonTitle
|
||||
cancelButtonBlock:(nullable JXTAlertClickBlock)cancelBlock
|
||||
otherButtonBlock:(nullable JXTAlertClickBlock)otherBlock;
|
||||
|
||||
|
||||
/**
|
||||
JXTAlertView: 不定数量按钮alert
|
||||
|
||||
@param title title
|
||||
@param message message
|
||||
@param cancelButtonTitle 取消按钮标题
|
||||
@param buttonIndexBlock 按钮回调
|
||||
@param otherButtonTitles 其他按钮标题列表
|
||||
*/
|
||||
+ (void)showAlertViewWithTitle:(nullable NSString *)title
|
||||
message:(nullable NSString *)message
|
||||
cancelButtonTitle:(nullable NSString *)cancelButtonTitle
|
||||
buttonIndexBlock:(nullable JXTAlertClickBlock)buttonIndexBlock
|
||||
otherButtonTitles:(nullable NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
|
||||
|
||||
|
||||
/**
|
||||
JXTAlertView: 不带按钮自动消失的toast
|
||||
|
||||
@param title title
|
||||
@param message message
|
||||
@param duration 显示时间
|
||||
@param dismissCompletion 关闭后回调
|
||||
*/
|
||||
+ (void)showToastViewWithTitle:(nullable NSString *)title
|
||||
message:(nullable NSString *)message
|
||||
duration:(NSTimeInterval)duration
|
||||
dismissCompletion:(nullable JXTAlertClickBlock)dismissCompletion;
|
||||
|
||||
|
||||
/**
|
||||
JXTAlertView: 文字HUD
|
||||
|
||||
@param title title
|
||||
@param message message
|
||||
*/
|
||||
+ (void)showTextHUDWithTitle:(nullable NSString *)title
|
||||
message:(nullable NSString *)message;
|
||||
|
||||
|
||||
/**
|
||||
JXTAlertView: loadHUD
|
||||
|
||||
@param title title
|
||||
@param message message
|
||||
*/
|
||||
+ (void)showLoadingHUDWithTitle:(nullable NSString *)title
|
||||
message:(nullable NSString *)message;
|
||||
|
||||
|
||||
/**
|
||||
JXTAlertView: progressHUD
|
||||
|
||||
@param title title
|
||||
@param message message
|
||||
*/
|
||||
+ (void)showProgressHUDWithTitle:(nullable NSString *)title
|
||||
message:(nullable NSString *)message;
|
||||
/**
|
||||
JXTAlertView: progressHUD,进度条进度值
|
||||
|
||||
@param progress 进度值
|
||||
*/
|
||||
+ (void)setHUDProgress:(float)progress;
|
||||
|
||||
|
||||
/**
|
||||
JXTAlertView: HUD公用方法,设置成功状态
|
||||
|
||||
@param title title
|
||||
@param message message
|
||||
*/
|
||||
+ (void)setHUDSuccessStateWithTitle:(nullable NSString *)title
|
||||
message:(nullable NSString *)message;
|
||||
/**
|
||||
JXTAlertView: HUD公用方法,设置失败状态
|
||||
|
||||
@param title title
|
||||
@param message message
|
||||
*/
|
||||
+ (void)setHUDFailStateWithTitle:(nullable NSString *)title
|
||||
message:(nullable NSString *)message;
|
||||
/**
|
||||
JXTAlertView: HUD公用方法,关闭HUD
|
||||
*/
|
||||
+ (void)dismissHUD;
|
||||
|
||||
@end
|
||||
+544
@@ -0,0 +1,544 @@
|
||||
//
|
||||
// JXTAlertView.m
|
||||
// JXTAlertManager
|
||||
//
|
||||
// Created by JXT on 2016/12/20.
|
||||
// Copyright © 2016年 JXT. All rights reserved.
|
||||
//
|
||||
|
||||
#import "JXTAlertView.h"
|
||||
|
||||
#pragma mark - Private
|
||||
/**
|
||||
* 取消按钮默认标题
|
||||
*/
|
||||
static NSString *const JXTCancelButtonTitleDefault = @"确定";
|
||||
/**
|
||||
* toast默认展示时间,当设置为0时,用该值
|
||||
*/
|
||||
static NSTimeInterval const JXTToastShowDurationDefault = 1.0f;
|
||||
/**
|
||||
* alertView子视图key
|
||||
*/
|
||||
static NSString *const JXTAlertViewAccessoryViewKey = @"accessoryView";
|
||||
|
||||
|
||||
#pragma mark - Public
|
||||
//1.常规alert
|
||||
void jxt_showAlertTwoButton(NSString *title, NSString *message, NSString *cancelButtonTitle, JXTAlertClickBlock cancelBlock, NSString *otherButtonTitle, JXTAlertClickBlock otherBlock)
|
||||
{
|
||||
jxt_getSafeMainQueue(^{
|
||||
[JXTAlertView showAlertViewWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitle:otherButtonTitle cancelButtonBlock:cancelBlock otherButtonBlock:otherBlock];
|
||||
});
|
||||
}
|
||||
void jxt_showAlertOneButton(NSString *title, NSString *message, NSString *cancelButtonTitle, JXTAlertClickBlock cancelBlock)
|
||||
{
|
||||
jxt_showAlertTwoButton(title, message, cancelButtonTitle, cancelBlock, nil, NULL);
|
||||
}
|
||||
void jxt_showAlertTitle(NSString *title)
|
||||
{
|
||||
jxt_showAlertTwoButton(title, nil, JXTCancelButtonTitleDefault, NULL, nil, NULL);
|
||||
}
|
||||
void jxt_showAlertMessage(NSString *message)
|
||||
{
|
||||
jxt_showAlertTwoButton(@"", message, JXTCancelButtonTitleDefault, NULL, nil, NULL);
|
||||
}
|
||||
void jxt_showAlertTitleMessage(NSString *title, NSString *message)
|
||||
{
|
||||
jxt_showAlertTwoButton(title, message, JXTCancelButtonTitleDefault, NULL, nil, NULL);
|
||||
}
|
||||
|
||||
//2.无按钮toast
|
||||
void jxt_showToastTitleMessageDismiss(NSString *title, NSString *message, NSTimeInterval duration, JXTAlertClickBlock dismissCompletion)
|
||||
{
|
||||
jxt_getSafeMainQueue(^{
|
||||
[JXTAlertView showToastViewWithTitle:title message:message duration:duration dismissCompletion:dismissCompletion];
|
||||
});
|
||||
}
|
||||
void jxt_showToastTitleDismiss(NSString *title, NSTimeInterval duration, JXTAlertClickBlock dismissCompletion)
|
||||
{
|
||||
jxt_showToastTitleMessageDismiss(title, nil, duration, dismissCompletion);
|
||||
}
|
||||
void jxt_showToastMessageDismiss(NSString *message, NSTimeInterval duration, JXTAlertClickBlock dismissCompletion)
|
||||
{
|
||||
jxt_showToastTitleMessageDismiss(@"", message, duration, dismissCompletion);
|
||||
}
|
||||
void jxt_showToastTitle(NSString *title, NSTimeInterval duration)
|
||||
{
|
||||
jxt_showToastTitleMessageDismiss(title, nil, duration, NULL);
|
||||
}
|
||||
void jxt_showToastMessage(NSString *message, NSTimeInterval duration)
|
||||
{
|
||||
jxt_showToastTitleMessageDismiss(@"", message, duration, NULL);
|
||||
}
|
||||
|
||||
//3.文字HUD
|
||||
void jxt_showTextHUDTitleMessage(NSString *title, NSString *message)
|
||||
{
|
||||
jxt_getSafeMainQueue(^{
|
||||
[JXTAlertView showTextHUDWithTitle:title message:message];
|
||||
});
|
||||
}
|
||||
void jxt_showTextHUDTitle(NSString *title)
|
||||
{
|
||||
jxt_showTextHUDTitleMessage(title, nil);
|
||||
}
|
||||
void jxt_showTextHUDMessage(NSString *message)
|
||||
{
|
||||
jxt_showTextHUDTitleMessage(@"", message);
|
||||
}
|
||||
|
||||
//4.loadHUD
|
||||
void jxt_showLoadingHUDTitleMessage(NSString *title, NSString *message)
|
||||
{
|
||||
jxt_getSafeMainQueue(^{
|
||||
[JXTAlertView showLoadingHUDWithTitle:title message:message];
|
||||
});
|
||||
}
|
||||
void jxt_showLoadingHUDTitle(NSString *title)
|
||||
{
|
||||
jxt_showLoadingHUDTitleMessage(title, nil);
|
||||
}
|
||||
void jxt_showLoadingHUDMessage(NSString *message)
|
||||
{
|
||||
jxt_showLoadingHUDTitleMessage(@"", message);
|
||||
}
|
||||
|
||||
//5.progressHUD
|
||||
void jxt_showProgressHUDTitleMessage(NSString *title, NSString *message)
|
||||
{
|
||||
jxt_getSafeMainQueue(^{
|
||||
[JXTAlertView showProgressHUDWithTitle:title message:message];
|
||||
});
|
||||
}
|
||||
void jxt_showProgressHUDTitle(NSString *title)
|
||||
{
|
||||
jxt_showProgressHUDTitleMessage(title, nil);
|
||||
}
|
||||
void jxt_showProgressHUDMessage(NSString *message)
|
||||
{
|
||||
jxt_showProgressHUDTitleMessage(@"", message);
|
||||
}
|
||||
void jxt_setHUDProgress(float progress)
|
||||
{
|
||||
[JXTAlertView setHUDProgress:progress];
|
||||
}
|
||||
|
||||
//6.HUD公用
|
||||
//成功状态
|
||||
void jxt_setHUDSuccessTitleMessage(NSString *title, NSString *message)
|
||||
{
|
||||
jxt_getSafeMainQueue(^{
|
||||
[JXTAlertView setHUDSuccessStateWithTitle:title message:message];
|
||||
});
|
||||
}
|
||||
void jxt_setHUDSuccessTitle(NSString *title)
|
||||
{
|
||||
jxt_setHUDSuccessTitleMessage(title, nil);
|
||||
}
|
||||
void jxt_setHUDSuccessMessage(NSString *message)
|
||||
{
|
||||
jxt_setHUDSuccessTitleMessage(@"", message);
|
||||
}
|
||||
//失败状态
|
||||
void jxt_setHUDFailTitleMessage(NSString *title, NSString *message)
|
||||
{
|
||||
jxt_getSafeMainQueue(^{
|
||||
[JXTAlertView setHUDFailStateWithTitle:title message:message];
|
||||
});
|
||||
}
|
||||
void jxt_setHUDFailTitle(NSString *title)
|
||||
{
|
||||
jxt_setHUDFailTitleMessage(title, nil);
|
||||
}
|
||||
void jxt_setHUDFailMessage(NSString *message)
|
||||
{
|
||||
jxt_setHUDFailTitleMessage(@"", message);
|
||||
}
|
||||
//关闭HUD
|
||||
void jxt_dismissHUD(void)
|
||||
{
|
||||
jxt_getSafeMainQueue(^{
|
||||
[JXTAlertView dismissHUD];
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - define
|
||||
/**
|
||||
* JXTAlertType
|
||||
*/
|
||||
typedef NS_ENUM(NSInteger, JXTAlertType) {
|
||||
JXTAlertTypeNormal,
|
||||
JXTAlertTypeToast,
|
||||
JXTAlertTypeHUD
|
||||
};
|
||||
|
||||
/**
|
||||
* JXTAlertHUDType
|
||||
*/
|
||||
typedef NS_ENUM(NSInteger, JXTAlertHUDType) {
|
||||
JXTAlertHUDTypeTextOnly,
|
||||
JXTAlertHUDTypeLoading,
|
||||
JXTAlertHUDTypeProgress
|
||||
};
|
||||
|
||||
|
||||
@interface JXTAlertView () <UIAlertViewDelegate>
|
||||
//block
|
||||
@property (nonatomic, copy) JXTAlertClickBlock buttonClickBlock;
|
||||
@property (nonatomic, copy) JXTAlertClickBlock completionBlock;
|
||||
//type
|
||||
@property (nonatomic, assign) JXTAlertType alertType;
|
||||
@property (nonatomic, assign) JXTAlertHUDType alertHUDType;
|
||||
//HUD附件
|
||||
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
|
||||
@property (nonatomic, strong) UIProgressView *progressView;
|
||||
|
||||
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle, ... NS_REQUIRES_NIL_TERMINATION;
|
||||
|
||||
@end
|
||||
|
||||
@implementation JXTAlertView
|
||||
|
||||
#pragma mark - Init
|
||||
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle, ...
|
||||
{
|
||||
self = [self initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitle, nil];
|
||||
if (!self) return nil;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - shared
|
||||
static JXTAlertView *__jxt_commonHUD = nil;
|
||||
+ (instancetype)sharedCommonHUDWithHUDType:(JXTAlertHUDType)HUDType
|
||||
{
|
||||
if (__jxt_commonHUD == nil)
|
||||
{
|
||||
__jxt_commonHUD = [[JXTAlertView alloc] initWithTitle:nil message:nil cancelButtonTitle:nil otherButtonTitle:nil];
|
||||
//
|
||||
__jxt_commonHUD.alertType = JXTAlertTypeHUD;
|
||||
__jxt_commonHUD.alertHUDType = HUDType;
|
||||
|
||||
switch (HUDType)
|
||||
{
|
||||
case JXTAlertHUDTypeTextOnly:
|
||||
break;
|
||||
case JXTAlertHUDTypeLoading:
|
||||
{
|
||||
//添加指示器
|
||||
UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
|
||||
indicatorView.color = [UIColor blackColor];
|
||||
[indicatorView startAnimating];
|
||||
__jxt_commonHUD.indicatorView = indicatorView;
|
||||
//强制添加子视图
|
||||
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
|
||||
{
|
||||
[__jxt_commonHUD setValue:indicatorView forKey:JXTAlertViewAccessoryViewKey];
|
||||
}
|
||||
else
|
||||
{
|
||||
[__jxt_commonHUD addSubview:indicatorView];
|
||||
}
|
||||
break;
|
||||
}
|
||||
case JXTAlertHUDTypeProgress:
|
||||
{
|
||||
//添加进度条
|
||||
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
|
||||
progressView.progressTintColor = [UIColor blackColor];
|
||||
progressView.progress = 0.0;
|
||||
__jxt_commonHUD.progressView = progressView;
|
||||
//强制添加子视图
|
||||
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
|
||||
{
|
||||
[__jxt_commonHUD setValue:progressView forKey:JXTAlertViewAccessoryViewKey];
|
||||
}
|
||||
else
|
||||
{
|
||||
[__jxt_commonHUD addSubview:progressView];
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return __jxt_commonHUD;
|
||||
}
|
||||
+ (JXTAlertView *)sharedCommonHUD
|
||||
{
|
||||
return __jxt_commonHUD;
|
||||
}
|
||||
+ (void)clearCommonHUD
|
||||
{
|
||||
__jxt_commonHUD = nil;
|
||||
}
|
||||
|
||||
//重写setValue:forUndefinedKey:方法,处理不存在的key赋值,防止崩溃
|
||||
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
|
||||
{
|
||||
NSLog(@"key: %@ 不存在", key);
|
||||
}
|
||||
- (id)valueForUndefinedKey:(NSString *)key
|
||||
{
|
||||
NSLog(@"value: %@ 不存在", key);
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - Methods
|
||||
//1.常规alert
|
||||
+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle cancelButtonBlock:(JXTAlertClickBlock)cancelBlock otherButtonBlock:(JXTAlertClickBlock)otherBlock
|
||||
{
|
||||
if (!(title.length > 0) && message.length > 0) {
|
||||
title = @"";
|
||||
}
|
||||
JXTAlertView *alertView = [[JXTAlertView alloc] initWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitle:otherButtonTitle, nil];
|
||||
|
||||
alertView.alertType = JXTAlertTypeNormal;
|
||||
|
||||
alertView.buttonClickBlock = ^(NSInteger buttonIndex){
|
||||
if (buttonIndex == 0)
|
||||
{
|
||||
if (cancelBlock) {
|
||||
cancelBlock(buttonIndex);
|
||||
}
|
||||
}
|
||||
else if (buttonIndex == 1)
|
||||
{
|
||||
if (otherBlock) {
|
||||
otherBlock(buttonIndex);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
[alertView show];
|
||||
}
|
||||
//不定按钮
|
||||
+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle buttonIndexBlock:(JXTAlertClickBlock)buttonIndexBlock otherButtonTitles:(NSString *)otherButtonTitles, ...
|
||||
{
|
||||
if (!(title.length > 0) && message.length > 0) {
|
||||
title = @"";
|
||||
}
|
||||
JXTAlertView *alertView = [[JXTAlertView alloc] initWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitle:nil];
|
||||
|
||||
alertView.alertType = JXTAlertTypeNormal;
|
||||
alertView.buttonClickBlock = buttonIndexBlock;
|
||||
|
||||
if (otherButtonTitles)
|
||||
{
|
||||
va_list args;//定义一个指向个数可变的参数列表指针
|
||||
va_start(args, otherButtonTitles);//得到第一个可变参数地址
|
||||
for (NSString *arg = otherButtonTitles; arg != nil; arg = va_arg(args, NSString *))
|
||||
{
|
||||
[alertView addButtonWithTitle:arg];
|
||||
}
|
||||
va_end(args);//置空指针
|
||||
}
|
||||
|
||||
[alertView show];
|
||||
}
|
||||
|
||||
//2.无按钮toast
|
||||
+ (void)showToastViewWithTitle:(NSString *)title message:(NSString *)message duration:(NSTimeInterval)duration dismissCompletion:(JXTAlertClickBlock)dismissCompletion
|
||||
{
|
||||
if (!(title.length > 0) && message.length > 0) {
|
||||
title = @"";
|
||||
}
|
||||
JXTAlertView *toastView = [[JXTAlertView alloc] initWithTitle:title message:message cancelButtonTitle:nil otherButtonTitle:nil];
|
||||
|
||||
toastView.alertType = JXTAlertTypeToast;
|
||||
|
||||
toastView.completionBlock = ^(NSInteger buttonIndex){
|
||||
if (buttonIndex == 0)
|
||||
{
|
||||
if (dismissCompletion) {
|
||||
dismissCompletion(buttonIndex);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
[toastView show];
|
||||
|
||||
duration = duration > 0 ? duration : JXTToastShowDurationDefault;
|
||||
[toastView performSelector:@selector(dismissToastView:) withObject:toastView afterDelay:duration];
|
||||
}
|
||||
|
||||
- (void)dismissToastView:(UIAlertView *)toastView
|
||||
{
|
||||
[toastView dismissWithClickedButtonIndex:0 animated:YES];
|
||||
}
|
||||
|
||||
//3.文字HUD
|
||||
+ (void)showTextHUDWithTitle:(NSString *)title message:(NSString *)message
|
||||
{
|
||||
if (!(title.length > 0) && message.length > 0) {
|
||||
title = @"";
|
||||
}
|
||||
JXTAlertView *textHUD = [JXTAlertView sharedCommonHUDWithHUDType:JXTAlertHUDTypeTextOnly];
|
||||
|
||||
textHUD.title = title;
|
||||
textHUD.message = message;
|
||||
// textHUD.delegate = nil;
|
||||
|
||||
[textHUD show];
|
||||
}
|
||||
|
||||
//4.loadHUD
|
||||
+ (void)showLoadingHUDWithTitle:(NSString *)title message:(NSString *)message
|
||||
{
|
||||
if (!(title.length > 0) && message.length > 0) {
|
||||
title = @"";
|
||||
}
|
||||
JXTAlertView *loadingHUD = [JXTAlertView sharedCommonHUDWithHUDType:JXTAlertHUDTypeLoading];
|
||||
|
||||
loadingHUD.title = title;
|
||||
loadingHUD.message = message;
|
||||
|
||||
[loadingHUD show];
|
||||
}
|
||||
|
||||
//5.progressHUD
|
||||
+ (void)showProgressHUDWithTitle:(NSString *)title message:(NSString *)message
|
||||
{
|
||||
if (!(title.length > 0) && message.length > 0) {
|
||||
title = @"";
|
||||
}
|
||||
JXTAlertView *alertHUD = [JXTAlertView sharedCommonHUDWithHUDType:JXTAlertHUDTypeProgress];
|
||||
|
||||
alertHUD.title = title;
|
||||
alertHUD.message = message;
|
||||
|
||||
[alertHUD show];
|
||||
}
|
||||
+ (void)setHUDProgress:(float)progress
|
||||
{
|
||||
JXTAlertView *alertHUD = [JXTAlertView sharedCommonHUD];
|
||||
[alertHUD.progressView setProgress:progress animated:YES];
|
||||
|
||||
if (progress >= 1.0) {
|
||||
[alertHUD.progressView setProgress:1];
|
||||
// [alertHUD dismissWithClickedButtonIndex:0 animated:YES];
|
||||
}
|
||||
}
|
||||
|
||||
//6.HUD公用
|
||||
+ (void)setHUDSuccessStateWithTitle:(NSString *)title message:(NSString *)message
|
||||
{
|
||||
JXTAlertView *alertHUD = [JXTAlertView sharedCommonHUD];
|
||||
alertHUD.title = title;
|
||||
alertHUD.message = message;
|
||||
|
||||
switch (alertHUD.alertHUDType)
|
||||
{
|
||||
case JXTAlertHUDTypeTextOnly:
|
||||
break;
|
||||
case JXTAlertHUDTypeLoading:
|
||||
{
|
||||
[alertHUD.indicatorView stopAnimating];
|
||||
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
|
||||
{
|
||||
[alertHUD setValue:nil forKey:JXTAlertViewAccessoryViewKey];
|
||||
}
|
||||
else
|
||||
{
|
||||
[alertHUD.indicatorView removeFromSuperview];
|
||||
}
|
||||
alertHUD.indicatorView = nil;
|
||||
break;
|
||||
}
|
||||
case JXTAlertHUDTypeProgress:
|
||||
{
|
||||
[alertHUD.progressView setProgress:1 animated:YES];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
+ (void)setHUDFailStateWithTitle:(NSString *)title message:(NSString *)message
|
||||
{
|
||||
JXTAlertView *alertHUD = [JXTAlertView sharedCommonHUD];
|
||||
alertHUD.title = title;
|
||||
alertHUD.message = message;
|
||||
|
||||
switch (alertHUD.alertHUDType)
|
||||
{
|
||||
case JXTAlertHUDTypeTextOnly:
|
||||
break;
|
||||
case JXTAlertHUDTypeLoading:
|
||||
{
|
||||
[alertHUD.indicatorView stopAnimating];
|
||||
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
|
||||
{
|
||||
[alertHUD setValue:nil forKey:JXTAlertViewAccessoryViewKey];
|
||||
}
|
||||
else
|
||||
{
|
||||
[alertHUD.indicatorView removeFromSuperview];
|
||||
}
|
||||
alertHUD.indicatorView = nil;
|
||||
break;
|
||||
}
|
||||
case JXTAlertHUDTypeProgress:
|
||||
{
|
||||
[alertHUD.progressView setProgress:0 animated:YES];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
+ (void)dismissHUD
|
||||
{
|
||||
JXTAlertView *alertHUD = [JXTAlertView sharedCommonHUD];
|
||||
switch (alertHUD.alertHUDType)
|
||||
{
|
||||
case JXTAlertHUDTypeTextOnly:
|
||||
break;
|
||||
case JXTAlertHUDTypeLoading:
|
||||
{
|
||||
[alertHUD.indicatorView stopAnimating];
|
||||
alertHUD.indicatorView = nil;
|
||||
break;
|
||||
}
|
||||
case JXTAlertHUDTypeProgress:
|
||||
break;
|
||||
}
|
||||
[alertHUD dismissWithClickedButtonIndex:0 animated:YES];
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - Delegate
|
||||
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
|
||||
{
|
||||
if (self.buttonClickBlock) {
|
||||
self.buttonClickBlock(buttonIndex);
|
||||
}
|
||||
self.buttonClickBlock = NULL;//解除闭环
|
||||
}
|
||||
|
||||
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
|
||||
{
|
||||
if (self.completionBlock) {
|
||||
self.completionBlock(buttonIndex);
|
||||
}
|
||||
self.completionBlock = NULL;//解除闭环
|
||||
|
||||
switch (self.alertType)
|
||||
{
|
||||
case JXTAlertTypeNormal:
|
||||
break;
|
||||
|
||||
case JXTAlertTypeToast:
|
||||
{
|
||||
//清理performSelector,防止意外情况下的内存泄漏
|
||||
[NSObject cancelPreviousPerformRequestsWithTarget:alertView selector:@selector(dismissToastView:) object:alertView];
|
||||
break;
|
||||
}
|
||||
case JXTAlertTypeHUD:
|
||||
{
|
||||
//清理static
|
||||
[JXTAlertView clearCommonHUD];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// JXTAlertManagerHeader.h
|
||||
// JXTAlertManagerDemo
|
||||
//
|
||||
// Created by JXT on 2016/12/22.
|
||||
// Copyright © 2016年 JXT. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef JXTAlertManagerHeader_h
|
||||
#define JXTAlertManagerHeader_h
|
||||
|
||||
#import "JXTAlertView.h"
|
||||
|
||||
//以下适配,只做提示用,实际使用,如果要适配iOS7,对应方法还是需要自行适配
|
||||
//不然的话,可能因为这个宏,导致bug,因为新版xcode编译,对应方法是可以使用的(因为xcode的API存在),但是实际在对应系统上,可能就崩溃了
|
||||
#ifdef NSFoundationVersionNumber_iOS_8_0
|
||||
#import "JXTAlertController.h"
|
||||
#endif
|
||||
|
||||
#endif /* JXTAlertManagerHeader_h */
|
||||
@@ -76,6 +76,7 @@
|
||||
}
|
||||
|
||||
-(void)dealloc{
|
||||
|
||||
NSLog(@"%s dealloc",object_getClassName(self));
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user