ios 利用运行时来解决判断是否进入引导页

        给AppDelegate添加一个分类自定义一个启动的方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions ,实现load方法,在load方法里面拿到存储的系统版本与当前系统版本b比较。若版本比当前的低则进入引导页. 

 重要的方法粘贴如下:

@implementation AppDelegate (Guaid)

+ (void)load{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

NSString* lastVersion = [[NSUserDefaults standardUserDefaults] stringForKey:kLastVersionKey];

NSString* curtVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];

if ([curtVersion compare:lastVersion] == NSOrderedDescending) {

Method originMethod = class_getInstanceMethod(self.class, @selector(application:didFinishLaunchingWithOptions:));

Method customMethod = class_getInstanceMethod(self.class, @selector(guaid_application:didFinishLaunchingWithOptions:));

method_exchangeImplementations(originMethod, customMethod);

}

});

}

- (BOOL)guaid_application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.guaidWindow = [[UIWindow alloc] init];

self.guaidWindow.frame = self.guaidWindow.screen.bounds;

self.guaidWindow.backgroundColor = [UIColor clearColor];

self.guaidWindow.windowLevel = UIWindowLevelStatusBar + 1;

[self.guaidWindow makeKeyAndVisible];

KSGuaidViewController* vc = [[KSGuaidViewController alloc] init];

__weak typeof(self) weakSelf = self;

vc.shouldHidden = ^{

NSString* curtVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];

[[NSUserDefaults standardUserDefaults] setObject:curtVersion forKey:kLastVersionKey];

[[NSUserDefaults standardUserDefaults] synchronize];

[weakSelf.guaidWindow resignKeyWindow];

weakSelf.guaidWindow.hidden = YES;

weakSelf.guaidWindow = nil;

};

self.guaidWindow.rootViewController = vc;

return [self guaid_application:application didFinishLaunchingWithOptions:launchOptions];

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容