GUNAlert is a simple wrapper around UIAlertView and UIAlertController. For applications that need to keep backward compatibility with iOS versions < 8. It is designed as a simple drop in solution for most common use cases.
To run the example project, clone the repo, and run pod install
from the Example directory first.
Before using alerts you will need to instantiate GUNAlert
class and make sure it's reference will not disappear until the alert is handled.
Here's an example:
In your UIViewController
create an instance of GUNAlert
class and assign it as a strong
property.
@interface MyViewController ()
@property (strong, nonatomic) GUNAlert *alert;
@end
@implementation GUNViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.alert = [[GUNAlert alloc] initWithViewController: self];
}
@end
In most cases you will want to inform user about a fact and acknowledge it or make a decision. To facilitate this GUNAlert
provides two helper methods:
To instantiate 'Cancel' alert or in other words alert with one action use the following method:
[self.alert cancelAlertWithTitle:@"Success!"
message:@"You've just won a lottery."
cancelTitle:@"Yay!"
cancelHandler:^{
[self transferLargeSumOfMoney];
}];
To instantiate 'Cancel & Ok' alert or in other words alert with two actions use the following method:
[self.alert cancelOkAlertWithTitle:@"Your meal is ready!"
message:@"Do you want something to drink?"
cancelTitle:@"Not really"
cancelHandler:^{
[self prepareOrder];
}
okTitle:@"Sure I do"
okHandler:^{
[self addDrinkToOrder];
[self prepareOrder];
}];
First prepare an array with titles for each action.
NSArray *titles = @[
@"Yes",
@"No",
@"Maybe"
];
Then prepare an array of handlers for each action. Both arrays must have the same number of elements.
NSArray *handlers = @[
^{
[self makeACall]
},
^{
},
^{
if (arc4random_uniform(2)) {
[self makeACall]
};
},
];
Finally use both arrays in the following method:
[self.alert alertWithTitle:@"Hey I've just met you"
message:@"Call me"
titles:titles
handlers:handlers];
Tested in iOS 7.1 and iOS 8
GUNAlert is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "GUNAlert"
Copy GUNAlert.h and GUNAlert.m to your project and modify according to your preference.
Michał Taszycki, michal.taszycki@getbase.com
GUNAlert is available under the MIT license. See the LICENSE file for more info.