0

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:
                                                           [url
                                                              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;
    }
}];
if(isTime){
//rootView1
}else{
 //rootView2
}

How can I fix that? Any ideas?

iGatiTech
  • 2,306
  • 1
  • 21
  • 45
Rockers23
  • 775
  • 1
  • 12
  • 24
  • in the `-application:didFinishLaunchingWithOptions:` you must not do such thing, __your entire concept is totally wrong__. you need to finish lunching as fast as possible, and start your custom job network operations _after_. – holex Jun 24 '16 at 09:34

3 Answers3

2

Change your code as below.

 {   
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:
                                                               [url
                                                                  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"]) {
        //rootView1
        //Change RootVC to RootView1
    }
    else {
        //rootView2
         //Change RootVC to RootView2
    }

}];

//Add LoadingVC (Intermediate) RootVC

}

-(void) changeRootViewController:(UIViewController*) viewController {
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController: viewController];
}  
Amit Kalghatgi
  • 357
  • 3
  • 16
  • I did it and the app crashes reason : Application windows are expected to have a root view controller at the end of application launch. – Rockers23 Jun 24 '16 at 09:32
1

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.

Andrea
  • 26,120
  • 10
  • 85
  • 131
0

// try like this

isTime = NO;
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:
                                                       [url
                                                          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"]) {
    [self changeRootController:YES];
} else {
   [self changeRootController:NO];
 }
}];


-(void)changeRootController:(Bool)change {
   if(change){
       //rootView1
   } else{
     //rootView2
  }
}
Satyanarayana
  • 1,059
  • 6
  • 16