解决iOS8以下UIAlertController无法使用的问题

XDAlertController

最近在一个项目中遇到的这样的一个问题iOS8以下UIAlertController无法使用,XDAlertController我的一个解决方案

实现原理

在iOS7环境下调用UIAlertController会崩溃,所以通过判断系统的版本号来调用不同的API;         通过Method Swizzling替换原生的presentViewController和提供近似于原生API接口,让开发者感觉不出与原生API有什么差别。(Method Swizzling通过method_exchangeImplementations交换方法,交换时期在调用load的时候,里面涉及runtime相关知识,如果理解起来吃力,可以移步到这边文章了解 )       

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#import "XDAlertController.h"
...
XDAlertController *alert = [XDAlertController alertControllerWithTitle:@"我是actionsheet" message:@"123" preferredStyle:XDAlertControllerStyleActionSheet];
    XDAlertAction *action1 = [XDAlertAction actionWithTitle:@"default" style: XDAlertActionStyleDefault handler:^( XDAlertAction * _Nonnull action) {
        
    }];
    
    XDAlertAction *action2 = [XDAlertAction actionWithTitle:@"取消" style:XDAlertActionStyleCancel handler:^(XDAlertAction * _Nonnull action) {
        
    }];
    
    XDAlertAction *action3 = [XDAlertAction actionWithTitle:@"destructive" style:XDAlertActionStyleDestructive handler:^(XDAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    
    [self presentViewController:alert animated:YES completion:nil];

github地址

https://github.com/caixindong/XDAlertController 大家觉得喜欢就赏个star,有什么问题可以issue我或者在评论指出,相互学习

Comments