diff --git a/RMUniversalAlert.h b/RMUniversalAlert.h index 3ea8e48..f94b9c6 100644 --- a/RMUniversalAlert.h +++ b/RMUniversalAlert.h @@ -14,8 +14,13 @@ typedef void(^RMUniversalAlertCompletionBlock)(RMUniversalAlert * __nonnull alert, NSInteger buttonIndex); +typedef void(^ShowTipDismissBlock)(); + @interface RMUniversalAlert : NSObject + + + + (nonnull instancetype)showAlertInViewController:(nonnull UIViewController *)viewController withTitle:(nullable NSString *)title message:(nullable NSString *)message @@ -33,6 +38,8 @@ typedef void(^RMUniversalAlertCompletionBlock)(RMUniversalAlert * __nonnull aler popoverPresentationControllerBlock:(void(^ __nullable)(RMPopoverPresentationController * __nonnull popover))popoverPresentationControllerBlock tapBlock:(nullable RMUniversalAlertCompletionBlock)tapBlock; ++ (void)showAlertInViewControllerN:(nonnull UIViewController *)viewController + withMessage:(nullable NSString *)message withBlock:(nullable ShowTipDismissBlock) misBlock; -(void)dismissAlertAnimated:(BOOL)animated; @property (readonly, nonatomic) BOOL visible; diff --git a/RMUniversalAlert.m b/RMUniversalAlert.m index 1b7e998..64edbbc 100644 --- a/RMUniversalAlert.m +++ b/RMUniversalAlert.m @@ -20,9 +20,9 @@ @interface RMUniversalAlert () -@property (nonatomic) UIAlertController *alertController; -@property (nonatomic) UIAlertView *alertView; -@property (nonatomic) UIActionSheet *actionSheet; +@property (nonatomic,weak) UIAlertController *alertController; +@property (nonatomic,weak) UIAlertView *alertView; +@property (nonatomic,weak) UIActionSheet *actionSheet; @property (nonatomic, assign) BOOL hasCancelButton; @property (nonatomic, assign) BOOL hasDestructiveButton; @@ -32,6 +32,21 @@ @interface RMUniversalAlert () @implementation RMUniversalAlert +- (instancetype)init { + if (self = [super init]) { + [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; + } + return self; +} + +- (void)didEnterBackground { + [self dismissAlertAnimated:NO]; +} + +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + + (instancetype)showAlertInViewController:(UIViewController *)viewController withTitle:(NSString *)title message:(NSString *)message @@ -40,6 +55,9 @@ + (instancetype)showAlertInViewController:(UIViewController *)viewController otherButtonTitles:(NSArray *)otherButtonTitles tapBlock:(RMUniversalAlertCompletionBlock)tapBlock { +// if ([viewController.presentedViewController isKindOfClass:[UIAlertController class]]) { +// [viewController.presentedViewController dismissViewControllerAnimated:NO completion:nil]; +// } RMUniversalAlert *alert = [[RMUniversalAlert alloc] init]; alert.hasCancelButton = cancelButtonTitle != nil; @@ -191,6 +209,49 @@ + (instancetype)showActionSheetInViewController:(UIViewController *)viewControll return alert; } + ++ (void)showAlertInViewControllerN:(nonnull UIViewController *)viewController + withMessage:(nullable NSString *)message withBlock:(ShowTipDismissBlock) misBlock { + + if ([UIAlertController class]) { + __block UIAlertController *alertController = [UIAlertController showAlertInViewController:viewController + withTitle:nil message:message + cancelButtonTitle:nil + destructiveButtonTitle:nil + otherButtonTitles:nil + tapBlock:nil]; + + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [alertController dismissViewControllerAnimated:YES completion:^{ + if (misBlock) { + misBlock(); + alertController = nil; + } + }]; + }); + + } else { + __block UIAlertView *alertView = [UIAlertView showWithTitle:nil + message:message + cancelButtonTitle:nil + otherButtonTitles:nil + tapBlock:nil]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [alertView dismissWithClickedButtonIndex:[alertView cancelButtonIndex] animated:YES]; + if (misBlock) { + misBlock(); + alertView = nil; + } + }); + } +} + + + + + + + #pragma mark - -(void)dismissAlertAnimated:(BOOL)animated { @@ -244,4 +305,5 @@ - (NSInteger)destructiveButtonIndex return RMUniversalAlertDestructiveButtonIndex; } + @end diff --git a/Tests/RMUniversalAlert.xcodeproj/project.pbxproj b/Tests/RMUniversalAlert.xcodeproj/project.pbxproj index f3901b9..6bfa43d 100644 --- a/Tests/RMUniversalAlert.xcodeproj/project.pbxproj +++ b/Tests/RMUniversalAlert.xcodeproj/project.pbxproj @@ -8,6 +8,14 @@ /* Begin PBXBuildFile section */ 16934E8D69700F79028CB83D /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1F25F13F1C0362E837894225 /* libPods.a */; }; + 4003982F1CD9993700E6BF3C /* NSObject+MemoryLeak.m in Sources */ = {isa = PBXBuildFile; fileRef = 400398221CD9993600E6BF3C /* NSObject+MemoryLeak.m */; }; + 400398301CD9993700E6BF3C /* UINavigationController+MemoryLeak.m in Sources */ = {isa = PBXBuildFile; fileRef = 400398241CD9993600E6BF3C /* UINavigationController+MemoryLeak.m */; }; + 400398311CD9993700E6BF3C /* UIPageViewController+MemoryLeak.m in Sources */ = {isa = PBXBuildFile; fileRef = 400398261CD9993600E6BF3C /* UIPageViewController+MemoryLeak.m */; }; + 400398321CD9993700E6BF3C /* UISplitViewController+MemoryLeak.m in Sources */ = {isa = PBXBuildFile; fileRef = 400398281CD9993600E6BF3C /* UISplitViewController+MemoryLeak.m */; }; + 400398331CD9993700E6BF3C /* UITabBarController+MemoryLeak.m in Sources */ = {isa = PBXBuildFile; fileRef = 4003982A1CD9993600E6BF3C /* UITabBarController+MemoryLeak.m */; }; + 400398341CD9993700E6BF3C /* UIView+MemoryLeak.m in Sources */ = {isa = PBXBuildFile; fileRef = 4003982C1CD9993600E6BF3C /* UIView+MemoryLeak.m */; }; + 400398351CD9993700E6BF3C /* UIViewController+MemoryLeak.m in Sources */ = {isa = PBXBuildFile; fileRef = 4003982E1CD9993600E6BF3C /* UIViewController+MemoryLeak.m */; }; + 400398381CD9996400E6BF3C /* SSViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 400398371CD9996400E6BF3C /* SSViewController.m */; }; 9408ECD71A1C6EED00A5A792 /* RMUniversalAlert.m in Sources */ = {isa = PBXBuildFile; fileRef = 9408ECD61A1C6EED00A5A792 /* RMUniversalAlert.m */; }; 94318CA91A60C88F0030FA5C /* RMPopoverPresentationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 94318CA81A60C88F0030FA5C /* RMPopoverPresentationController.m */; }; 944A77DC1A1C6D14002BBBED /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 944A77DB1A1C6D14002BBBED /* main.m */; }; @@ -31,6 +39,23 @@ /* Begin PBXFileReference section */ 1F25F13F1C0362E837894225 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; }; + 400398201CD9993600E6BF3C /* MLeaksFinder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MLeaksFinder.h; sourceTree = ""; }; + 400398211CD9993600E6BF3C /* NSObject+MemoryLeak.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+MemoryLeak.h"; sourceTree = ""; }; + 400398221CD9993600E6BF3C /* NSObject+MemoryLeak.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+MemoryLeak.m"; sourceTree = ""; }; + 400398231CD9993600E6BF3C /* UINavigationController+MemoryLeak.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UINavigationController+MemoryLeak.h"; sourceTree = ""; }; + 400398241CD9993600E6BF3C /* UINavigationController+MemoryLeak.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UINavigationController+MemoryLeak.m"; sourceTree = ""; }; + 400398251CD9993600E6BF3C /* UIPageViewController+MemoryLeak.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIPageViewController+MemoryLeak.h"; sourceTree = ""; }; + 400398261CD9993600E6BF3C /* UIPageViewController+MemoryLeak.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIPageViewController+MemoryLeak.m"; sourceTree = ""; }; + 400398271CD9993600E6BF3C /* UISplitViewController+MemoryLeak.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UISplitViewController+MemoryLeak.h"; sourceTree = ""; }; + 400398281CD9993600E6BF3C /* UISplitViewController+MemoryLeak.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UISplitViewController+MemoryLeak.m"; sourceTree = ""; }; + 400398291CD9993600E6BF3C /* UITabBarController+MemoryLeak.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITabBarController+MemoryLeak.h"; sourceTree = ""; }; + 4003982A1CD9993600E6BF3C /* UITabBarController+MemoryLeak.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITabBarController+MemoryLeak.m"; sourceTree = ""; }; + 4003982B1CD9993600E6BF3C /* UIView+MemoryLeak.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+MemoryLeak.h"; sourceTree = ""; }; + 4003982C1CD9993600E6BF3C /* UIView+MemoryLeak.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+MemoryLeak.m"; sourceTree = ""; }; + 4003982D1CD9993600E6BF3C /* UIViewController+MemoryLeak.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+MemoryLeak.h"; sourceTree = ""; }; + 4003982E1CD9993600E6BF3C /* UIViewController+MemoryLeak.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+MemoryLeak.m"; sourceTree = ""; }; + 400398361CD9996400E6BF3C /* SSViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SSViewController.h; sourceTree = ""; }; + 400398371CD9996400E6BF3C /* SSViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SSViewController.m; sourceTree = ""; }; 4A55E5ABB1C21D6685D020A0 /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = ""; }; 9408ECD51A1C6EED00A5A792 /* RMUniversalAlert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RMUniversalAlert.h; path = ../../RMUniversalAlert.h; sourceTree = ""; }; 9408ECD61A1C6EED00A5A792 /* RMUniversalAlert.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RMUniversalAlert.m; path = ../../RMUniversalAlert.m; sourceTree = ""; }; @@ -71,6 +96,28 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 4003981F1CD9992500E6BF3C /* ll */ = { + isa = PBXGroup; + children = ( + 400398201CD9993600E6BF3C /* MLeaksFinder.h */, + 400398211CD9993600E6BF3C /* NSObject+MemoryLeak.h */, + 400398221CD9993600E6BF3C /* NSObject+MemoryLeak.m */, + 400398231CD9993600E6BF3C /* UINavigationController+MemoryLeak.h */, + 400398241CD9993600E6BF3C /* UINavigationController+MemoryLeak.m */, + 400398251CD9993600E6BF3C /* UIPageViewController+MemoryLeak.h */, + 400398261CD9993600E6BF3C /* UIPageViewController+MemoryLeak.m */, + 400398271CD9993600E6BF3C /* UISplitViewController+MemoryLeak.h */, + 400398281CD9993600E6BF3C /* UISplitViewController+MemoryLeak.m */, + 400398291CD9993600E6BF3C /* UITabBarController+MemoryLeak.h */, + 4003982A1CD9993600E6BF3C /* UITabBarController+MemoryLeak.m */, + 4003982B1CD9993600E6BF3C /* UIView+MemoryLeak.h */, + 4003982C1CD9993600E6BF3C /* UIView+MemoryLeak.m */, + 4003982D1CD9993600E6BF3C /* UIViewController+MemoryLeak.h */, + 4003982E1CD9993600E6BF3C /* UIViewController+MemoryLeak.m */, + ); + name = ll; + sourceTree = ""; + }; 944A77CD1A1C6D14002BBBED = { isa = PBXGroup; children = ( @@ -94,6 +141,7 @@ 944A77D81A1C6D14002BBBED /* RMUniversalAlert */ = { isa = PBXGroup; children = ( + 4003981F1CD9992500E6BF3C /* ll */, 9408ECD51A1C6EED00A5A792 /* RMUniversalAlert.h */, 9408ECD61A1C6EED00A5A792 /* RMUniversalAlert.m */, 94318CA71A60C88F0030FA5C /* RMPopoverPresentationController.h */, @@ -106,6 +154,8 @@ 944A77E61A1C6D14002BBBED /* Images.xcassets */, 944A77E81A1C6D14002BBBED /* LaunchScreen.xib */, 944A77D91A1C6D14002BBBED /* Supporting Files */, + 400398361CD9996400E6BF3C /* SSViewController.h */, + 400398371CD9996400E6BF3C /* SSViewController.m */, ); path = RMUniversalAlert; sourceTree = ""; @@ -165,6 +215,7 @@ 944A77D31A1C6D14002BBBED /* Frameworks */, 944A77D41A1C6D14002BBBED /* Resources */, 43721C4A8E55A66EDAE8BC77 /* Copy Pods Resources */, + 3F70E5280D6D7D2BE53AC99D /* Embed Pods Frameworks */, ); buildRules = ( ); @@ -251,6 +302,21 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ + 3F70E5280D6D7D2BE53AC99D /* Embed Pods Frameworks */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + name = "Embed Pods Frameworks"; + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods/Pods-frameworks.sh\"\n"; + showEnvVarsInLog = 0; + }; 43721C4A8E55A66EDAE8BC77 /* Copy Pods Resources */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; @@ -289,10 +355,18 @@ buildActionMask = 2147483647; files = ( 944A77E21A1C6D14002BBBED /* ViewController.m in Sources */, + 400398301CD9993700E6BF3C /* UINavigationController+MemoryLeak.m in Sources */, + 400398351CD9993700E6BF3C /* UIViewController+MemoryLeak.m in Sources */, + 4003982F1CD9993700E6BF3C /* NSObject+MemoryLeak.m in Sources */, 9408ECD71A1C6EED00A5A792 /* RMUniversalAlert.m in Sources */, + 400398381CD9996400E6BF3C /* SSViewController.m in Sources */, 944A77DF1A1C6D14002BBBED /* AppDelegate.m in Sources */, + 400398341CD9993700E6BF3C /* UIView+MemoryLeak.m in Sources */, + 400398311CD9993700E6BF3C /* UIPageViewController+MemoryLeak.m in Sources */, + 400398321CD9993700E6BF3C /* UISplitViewController+MemoryLeak.m in Sources */, 944A77DC1A1C6D14002BBBED /* main.m in Sources */, 94318CA91A60C88F0030FA5C /* RMPopoverPresentationController.m in Sources */, + 400398331CD9993700E6BF3C /* UITabBarController+MemoryLeak.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };