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];

}

你可能感兴趣的:(ios 利用运行时来解决判断是否进入引导页)