To address backwards compatibility for UIAlertView
, I have come up with an interface
that would seem to work for both iOS versions 8 and lower.
This is not something I would like to roll out in production hence I would appreciate few extra pair of eyes taking a look at it.
Here is a link to the GitHub gist.
//UIAlertMode.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef void (^AlertViewSelectionBlock) (NSInteger);
typedef void (^AlertControllerDefaultAction ) (UIAlertAction *);
typedef void (^AlertControllerCancelAction) (UIAlertAction *);
typedef void (^GenericBlock) (void *);
@interface UIAlertMode : NSObject
@property (assign, nonatomic) AlertViewSelectionBlock alertViewBlock;
@property (assign, nonatomic) AlertControllerCancelAction cancelAlertAction;
@property (assign, nonatomic) AlertControllerDefaultAction defaultAlertAction;
- (id) initAlertWith:(NSString *) title
:(NSString *) message
:(NSString *) cancelButtonTitle
:(NSString *) otherButtonTitle;
- (BOOL) supportsiOS8;
- (void) showAlertFor:(AlertViewSelectionBlock) alertViewBlock;
- (void) showAlertControllerFor:(AlertControllerDefaultAction) defaultAction :(AlertControllerCancelAction) cancelAction;
- (void) showAlertWith:(GenericBlock) block;
@end
// UIAlertMode.m
#import "UIAlertMode.h"
#import <objc/objc-runtime.h>
#import "AppDelegate.h"
@interface UIAlertMode () <UIAlertViewDelegate> {
UIAlertView *_alertView;
UIAlertController *_alertController;
}
@end
@implementation UIAlertMode
@synthesize alertViewBlock;
@synthesize defaultAlertAction;
@synthesize cancelAlertAction;
- (id) init {
self = [super init];
return self;
}
- (id) initAlertWith:(NSString *) title
:(NSString *) message
:(NSString *) cancelButtonTitle
:(NSString *) otherButtonTitle {
self = [super init];
if ([self supportsiOS8]) {
_alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if (self.defaultAlertAction) {
self.defaultAlertAction(action);
}
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
if (self.cancelAlertAction) {
self.cancelAlertAction(action);
}
}];
[_alertController addAction:defaultAction];
[_alertController addAction:cancelAction];
} else {
_alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitle, nil];
}
return self;
}
#pragma mark - Public API
- (BOOL) supportsiOS8 {
Class kAlertViewClass = NSClassFromString(@"UIAlertController");
return !kAlertViewClass ? NO : YES;
}
- (void) showAlertFor:(AlertViewSelectionBlock) alertBlock {
self.alertViewBlock = alertBlock;
[_alertView show];
}
- (void) showAlertControllerFor:(AlertControllerDefaultAction) defaultAction :(AlertControllerCancelAction) cancelAction {
self.cancelAlertAction = cancelAction;
self.defaultAlertAction = defaultAction;
dispatch_async(dispatch_get_main_queue(), ^{
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.window.rootViewController presentViewController:_alertController animated:YES completion:nil];
});
}
#pragma mark - UIAlertView delegate method
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (self.alertViewBlock) {
self.alertViewBlock(buttonIndex);
}
}
I would like to get feedback on design, scalability, and memory management.