ASJAlertController 1.3

ASJAlertController 1.3

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Jan 2022

Maintained by Sudeep.



  • By
  • Sudeep

ASJAlertController

UIAlertController is a major improvement over the good old UIAlertView. It doesn't however have a simple show method to make it appear on the screen and must be presented on a view controller. Like me, if you miss the old way of showing alerts, this little library will be of help.

This is a category on UIAlertController that adds the show method and a few convenience methods to create alerts and action sheets.

It works by creating a new UIWindow over the app's key window and presenting a UIAlertController on it. This is the method Apple supposedly uses in their own implementation.

Installation

CocoaPods is the preferred way to install this library. Add this command to your Podfile:

pod 'ASJAlertController'

Usage

Show alert

[ASJAlertController showAlertWithTitle:@"Title"
                                 message:@"Message"
                       cancelButtonTitle:@"Cancel"
                              tapHandler:^(ASJAlertAction * _Nullable action, NSString * _Nullable buttonTitle)
   {
     // handle button taps here
   }];

Show action sheet

[ASJAlertController showActionSheetWithTitle:@"Title"
                                       message:@"Message"
                             cancelButtonTitle:@"Cancel"
                        destructiveButtonTitle:@"Destroy"
                                   otherTitles:@[@"Button 1", @"Button 2"]
                                    tapHandler:^(ASJAlertAction * _Nullable action, NSString * _Nullable buttonTitle)
   {
     // handle button taps here
   }];

Show alert/action sheet using ASJAlertButtons

ASJAlertButton *button1 = [ASJAlertButton buttonWithTitle:@"Button 1" style:ASJAlertActionStyleDestructive];
ASJAlertButton *button2 = [ASJAlertButton buttonWithTitle:@"Button 2" style:ASJAlertActionStyleDefault];

[ASJAlertController showAlertWithTitle:@"Test"
                                 message:@"A test action sheet"
                       cancelButtonTitle:@"Cancel"
                            otherButtons:@[button1, button2]
                          preferredStyle:ASJAlertControllerStyleActionSheet
                              tapHandler:^(ASJAlertAction * _Nullable action, NSString * _Nullable buttonTitle)
   {
     // handle button taps here
   }];

Show manually

You also have the option to not show the alert immediately. Use the show methods to do this. For otherButtons, you need to pass an NSArray of kind ASJAlertButtons or NSStrings, depending on the method you use. You may keep nil any arguments you don't require.

ASJAlertController *alert = [ASJAlertController alertWithTitle:@"Title"
                                                      message:@"Message"
                                            cancelButtonTitle:@"Cancel"
                                       destructiveButtonTitle:nil
                                                  otherTitles:nil
                                              preferredStyle:ASJAlertControllerStyleAlert
                                                  tapHandler:^(ASJAlertAction * _Nullable action, NSString * _Nullable buttonTitle)
                               {
                                 // handle button taps here...
                               }];
[alert show];

Credits

License

ASJAlertController is available under the MIT license. See the LICENSE file for more info.