Rockers23 Rockers23 - 1 year ago 111
iOS Question

iOS Change rootViewController Dynamically

I would like to change rootViewController dynamically. It depends on json response. I have a BOOL value , it changes after getting json response. The problem is that this value never change even when the condition is true.

my code is below , I put it in application didFinishLaunchingWithOptions :

isTime = NO;
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ]];

__block NSDictionary * json;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError){
json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&connectionError];
NSLog(@"JSON >>>> %@",[json objectForKey:@"status"]);
if ([[json objectForKey:@"status"] isEqualToString:@"ok"]) {
isTime = YES;

How can I fix that? Any ideas?

Answer Source

Is pretty simple, just change the window -rootViewController

-(void) changeRootViewController:(UIViewController*) vc {
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController: vc];

Since its been a long time from the last time I wrote in objC there could be syntax errors, but I think it can give you an idea.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download