nwkeeley
nwkeeley

Reputation: 1437

Problems calling performSegueWithIdentifier from connectionDidFinishLoading

I have a login screen and when a button is pressed this code executes:

- (IBAction)btnLogin:(id)sender {
    [username resignFirstResponder];
    [password resignFirstResponder];


    Auth *authRequest = [[Auth alloc] init];



    NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:APIHOST path:@"/authenticate"];
    [authRequest setUrl:url];
    [authRequest setUsername:[username text]];
    [authRequest setPassword:[password text]];
    [authRequest authenticate];

    [self performSegueWithIdentifier:@"Login2Tab" sender:self];


}

This works well and the segue executes as expected (So I know the segue exists and its identified correctly in the storyboard). I however don't want to perform the segue until the connection has finished loading so this implementation file is my delegate for NSURLConnection and at the bottom I have (and the if/else on responseCode does work)..:

 // Close the connection
 - (void)connectionDidFinishLoading:(NSURLConnection*)connection {
     if(responseCode == 200) {
         [self performSegueWithIdentifier:@"Login2Tab" sender:self];
     } else {
         NSLog(@"Bad.. bad.. bad...");
     }     
     NSLog(@"Connection Closed."); 
 }

(When I put the performSegueWithIdentifer in the connectionDidFinishLoading I comment out the performSegueWithIdentier from above...).

But now the app always crashes stating:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (LoginViewController: 0x7163640) has no segue with identifier 'Login2Tab''

Which is where I am stuck because it works when its not called from within the connectionDidFinishLoading...

Upvotes: 0

Views: 388

Answers (1)

nwkeeley
nwkeeley

Reputation: 1437

Just wanted to say I have this resolved altho I am not entirely sure how.

Basically this line in my ViewController

[authRequest authenticate];

Was loading a method in an external class file:

- (void)authenticate {
    NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:APIHOST path:@"/authenticate"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:3];
    NSString *postString = [[NSString alloc] initWithFormat:@"email=%@&password=%@", [username text], [password text]];

    [request setValue:APIKEY forHTTPHeaderField:@"apikey"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    [request setURL:url];


    if ([NSURLConnection canHandleRequest:request]) {
        NSLog(@"Starting network connection");
        NSURLConnection *myConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [myConnection start];
    } else {
        //Error cannot activate network request from device
    }
}

The delegates for the NSURLConnection were in the viewcontroller... and I could see them being accessed when the method was complete however I couldn't perform the segue from them. By moving this function from the class file to my viewcontroller I was able to call my segue's from the delegate method of connectionDidFinishLoading....

Strange but worked.

Upvotes: 0

Related Questions