KRNAlert 0.0.3

KRNAlert 0.0.3

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Mar 2017

Maintained by Julian Drapaylo.



KRNAlert 0.0.3

  • By
  • Julian Drapaylo

KRNAlert is a wrapper of UIAlertController to simplify its usage in typical cases

How to use

All methods of KRNAlert are static so you can simply call any of method without necessity of allocation of KRNAlert class instance. Every method requeries from you to pass an instance of UIViewController from which alert will be presented.

Alert Views

To present alert views you can use one of convenient methods described below:

+ (void)alertOKFrom:withTitle:message:completion:; // alert with custom title, message and one button with name "OK".
+ (void)alertOKCancelFrom:withTitle:message:completion:; //alert view with custom title, message and two buttons with name "OK" and "Cancel". Completion block is called if "OK" button pressed
+ (void)alertYesNoFrom:withTitle:message:yesCompletion:noCompletion:; //alert view with custom title, message and two buttons with name "YES" and "NO". 
+ (void)alertErrorFrom:withMessage:completion:; //alert view with title "ERROR", custom message and "OK" Button

For example if some button was pressed in your View Controller instance the usage of KRNAlert may look like below:

- (IBAction)buttonPressed:(id)sender {
    [KRNAlert alertOKFrom:self withTitle:@"ALERT" message:@"Everything is OK" completion:^{
        NSLog(@"OK Button pressed"); // completion will be called if button "OK" is pressed.
    }];
}

To present alert with one or two buttons with any title, message and buttons titles you can use next method:

+ (void)alertFrom:withTitle:message:firstButtonTitle:firstButtonCompletion:secondButtonTitle:secondButtonCompletion:;

The example:

- (IBAction)customAlertPressed:(id)sender {
    [KRNAlert alertFrom:self withTitle:@"Some title" message:@"Some message" firstButtonTitle:@"First Button" firstButtonCompletion:nil secondButtonTitle:@"Second button" secondButtonCompletion:nil]; // in this example completions are nil but in real app you probably would pass blocks which handle tapping on first of second button.
}

To present alert with as many buttons as you wish use method which takes an array of UIAlertActions instances:

+ (void)alertFrom:withTitle:message:andActions:;

The example of alert with five buttons written below:

- (IBAction)fiveButtonsActionSheet:(id)sender {
    NSMutableArray<UIAlertAction *> *actions = [NSMutableArray new];
    NSArray<NSString *> *names = @[@"One", @"Two", @"Three", @"Four", @"Five"];
    for (NSString *name in names) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:name style:UIAlertActionStyleDefault handler:nil];
        [actions addObject:action];
    }
    [KRNAlert actionSheetFrom:self withTitle:@"Five buttons action" message:@"Here is five buttons" andActions:[actions copy]];
}

There is also the method to present alert with one UITextField instance and handle entered text:

+ (void)alertOKCancelFrom:withTitle:message:textFieldPlaceholder:textFieldText:secureEntry:completion:;

Example below can be useful for alert with textField for password from user:

- (IBAction)alertWithTextField:(id)sender {
    [KRNAlert alertOKCancelFrom:self withTitle:@"Custom alert with textfield" message:@"Custom message" textFieldPlaceholder:@"Password" textFieldText:nil secureEntry:YES completion:^(NSString *textFieldString) {
        NSLog(@"Password is %@", textFieldString);
    }];
}

Actions sheets

Methods for presenting actions sheets are similar to methods for presenting alert views.

To present convenient method of action sheet for picking photo use next example:

- (IBAction)photoActionSheetPressed:(id)sender {
    [KRNAlert actionSheetFrom:self pickPhotofromGallery:^{
        NSLog(@"Gallery pressed"); // here move user to his photo gallery
    } andCamera:^{
        NSLog(@"Camera pressed"); // here check if camera is available and move user to UIImagePickerController or custom photo picker
    }];
}

To present action sheet with one or two buttons and required button "Cancel" with any title, message and buttons titles you can use next method:

+ (void)actionSheetFrom:withTitle:message:firstButtonTitle:firstButtonCompletion:secondButtonTitle:secondButtonCompletion:;

To present action sheet with as many buttons as you wish use method which takes an array of UIAlertActions instances:

+ (void)actionSheetFrom:withTitle:message:andActions:;

Redefining default button titles

You can redefine default alert button titles by using next methods:

+ (void)setOKButtonTitle:;
+ (void)setCancelButtonTitle:;
+ (void)setYes:andNoButtonTitles:;
+ (void)setErrorTitle:;
+ (void)setPhotoActionSheetTitle:andMessage:;
+ (void)setPhotoActionSheetGalleryButtonTitle:andCameraButtonTitle:;

Requirements

  • iOS 8.0 and above
  • XCode 8+
  • Example project will run only on iOS 9 and higher.

License

KRNAlert is released under the MIT license. See LICENSE for details.

Contact

Any suggestion or question? Please create a Github issue or reach me out.

LinkedIn