Wrapper class for UIAlertView / UIActionSheet / UIAlertController for targeting all iOS versions.
RMUniversalAlert is a wrapper class that builds upon UIAlertView+Blocks, UIActionSheet+Blocks, and UIAlertController+Blocks, and gives a simplified interface to support all iOS versions.
typedef void(^RMUniversalAlertCompletionBlock)(RMUniversalAlert *alert, NSInteger buttonIndex);
+ (instancetype)showAlertInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSArray *)otherButtonTitles
tapBlock:(RMUniversalAlertCompletionBlock)tapBlock;
+ (instancetype)showActionSheetInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSArray *)otherButtonTitles
popoverPresentationControllerBlock:(void(^)(RMPopoverPresentationController *popover))popoverPresentationControllerBlock
tapBlock:(RMUniversalAlertCompletionBlock)tapBlock;
The below code will show an alert on all iOS versions, and allow you to perform your logic in a single inline code path. On iOS 8 and above, it will use UIAlertController - giving you red text on the destructive button. On iOS 7 and earlier, it will use a standard UIAlertView.
[RMUniversalAlert showAlertInViewController:self
withTitle:@"Test Alert"
message:@"Test Message"
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Delete"
otherButtonTitles:@[@"First Other", @"Second Other"]
tapBlock:^(RMUniversalAlert *alert, NSInteger buttonIndex){
if (buttonIndex == alert.cancelButtonIndex) {
NSLog(@"Cancel Tapped");
} else if (buttonIndex == alert.destructiveButtonIndex) {
NSLog(@"Delete Tapped");
} else if (buttonIndex >= alert.firstOtherButtonIndex) {
NSLog(@"Other Button Index %ld", (long)buttonIndex - alert.firstOtherButtonIndex);
}
}];
RMUniversalAlert.showAlertInViewController(self,
withTitle: "Test Alert",
message: "Test Message",
cancelButtonTitle: "Cancel",
destructiveButtonTitle: "Delete",
otherButtonTitles: ["First Other", "Second Other"],
tapBlock: {(alert, buttonIndex) in
if (buttonIndex == alert.cancelButtonIndex) {
println("Cancel Tapped")
} else if (buttonIndex == alert.destructiveButtonIndex) {
println("Delete Tapped")
} else if (buttonIndex >= alert.firstOtherButtonIndex) {
println("Other Button Index \(buttonIndex - alert.firstOtherButtonIndex)")
}
})
iOS 6 and later. Since version 0.7 the headers use the new Objective-C nullability annotations for nicer interoperability with Swift, so you will need Xcode 6.3 or later to compile it.
The below code will show an action sheet on all iOS versions, and allow you to perform your logic in a single inline code path. On iOS 8 and above, it will use UIAlertController - giving you red text on the destructive button. On iOS 7 and earlier, it will use a standard UIActionSheet.
The popoverPresentationControllerBlock
allows you to configure the popover's source view/rect/bar button item if the action sheet will be on an iPad.
[RMUniversalAlert showActionSheetInViewController:self
withTitle:@"Test Action Sheet"
message:@"Test Message"
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Delete"
otherButtonTitles:@[@"First Other", @"Second Other"]
popoverPresentationControllerBlock:^(RMPopoverPresentationController *popover){
popover.sourceView = self.view;
popover.sourceRect = sender.frame;
}
tapBlock:^(RMUniversalAlert *alert, NSInteger buttonIndex){
if (buttonIndex == alert.cancelButtonIndex) {
NSLog(@"Cancel Tapped");
} else if (buttonIndex == alert.destructiveButtonIndex) {
NSLog(@"Delete Tapped");
} else if (buttonIndex >= alert.firstOtherButtonIndex) {
NSLog(@"Other Button Index %ld", (long)buttonIndex - alert.firstOtherButtonIndex);
}
}];
RMUniversalAlert.showActionSheetInViewController(self,
withTitle: "Test Action Sheet",
message: "Test Message",
cancelButtonTitle: "Cancel",
destructiveButtonTitle: "Delete",
otherButtonTitles: ["First Other", "Second Other"],
popoverPresentationControllerBlock: {(popover) in
popover.sourceView = self.view
popover.sourceRect = sender.frame
},
tapBlock: {(alert, buttonIndex) in
if (buttonIndex == alert.cancelButtonIndex) {
println("Cancel Tapped")
} else if (buttonIndex == alert.destructiveButtonIndex) {
println("Delete Tapped")
} else if (buttonIndex >= alert.firstOtherButtonIndex) {
println("Other Button Index \(buttonIndex - alert.firstOtherButtonIndex)")
}
})
pod 'RMUniversalAlert'
using CocoaPods.
Download this project, navigate to Tests
and run pod install
. Open RMUniversalAlert.xcworkspace
.