Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:04:29+00:00 2026-06-04T19:04:29+00:00

I am trying to create an authentication system in an iOS app that allows

  • 0

I am trying to create an authentication system in an iOS app that allows a user to log in as well as register if they do not already have an account. I got the login system completely up and running yesterday, but when I got the code set up for the registration system, the code would not even ping the server. I then tried to test the login system again, and the code will not ping the server now either.

Relevant code for the RegistrationTableViewController (it’s a custom TVC that contains text fields in some of the cells – think of the view to create a new calendar event, for example):

- (IBAction)signUpButtonPressed { 
    // Get the values out of the text fields that the user has filled out.
    NSString *email = self.emailTextField.text;
    NSString *firstName = self.firstNameTextField.text;
    NSString *lastName = self.lastNameTextField.text;
    NSString *password = self.passwordTextField.text;
    // Assuming that sign-up could potentially take a noticeable amount of time, run the
    // process on a separate thread to avoid locking the UI.
    dispatch_queue_t signUpQueue = dispatch_queue_create("sign-up authenticator", NULL);
    dispatch_async(signUpQueue, ^{
        // self.brain refers to a SignUpBrain property. See the code for the class below.
        [self.brain signUpUsingEmail:email firstName:firstName lastName:lastName  
          andPassword:password];  
        dispatch_async(dispatch_get_main_queue(), ^{  
            [self performSegueWithIdentifier:@"ShowMainFromSignUp" sender:self];  
        });  
    });  
    dispatch_release(signUpQueue);  
}

Relevant code for the SignUpBrain:

- (void)signUpUsingEmail:(NSString *)email firstName:(NSString *)firstName
                lastName:(NSString *)lastName andPassword:(NSString *)password {
    self.email = email;
    self.firstName = firstName;
    self.lastName = lastName;
    self.password = password;

    // Handle sign-up web calls.
    NSMutableURLRequest *signUpRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL 
      URLWithString:@"URL GOES HERE"]]; // obviously there's an actual URL in real code
    NSString *postString = [NSString stringWithFormat:@"uname=%@&pw=%@&fname=%@&lname=%@", 
      email, password, firstName, lastName];
//NSLog(postString);
    [signUpRequest setHTTPMethod:@"POST"];
    [signUpRequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *signUpConnection = 
      [NSURLConnection connectionWithRequest:signUpRequest delegate:self];
    [signUpConnection start];

    // Store any user data.
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    self.signUpResponse = data;
    NSError *error;
    NSDictionary *jsonLoginResults = [NSJSONSerialization JSONObjectWithData:data 
      options:0 error:&error];
    if (error) {
        NSLog(error.description);
    }
    NSLog(jsonLoginResults.description);

    // Return whether the user has successfully been registered.
    // If success is 1, then registration has been completed successfully. 0 if not.
    if ([jsonLoginResults objectForKey:@"status"]) {
        NSLog(@"Success!");
    }
}

I will also note that I created a test that uses these same web calls in a UIWebView, and it works successfully.

Please let me know if I need to clarify anything or include any more of the code! Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:04:30+00:00Added an answer on June 4, 2026 at 7:04 pm

    Under normal circumstances, you don’t need to run networking operations on a different thread.

    What you need to avoid is synchronous operation on the main thread. This will cause iOS to kill your application, since it will stop responding to events while the network transfer is busy.

    Apple’s suggestion to is to use asynchronous operation on the main thread, rather than synchronous operation on another thread.

    Using asynchronous operation will cause the networking itself to run in the background. You can consider this a dedicated thread by Apple. Your delegate methods will be called in the correct sequence on the main thread to catch up with networking as iOS determines it is appropriate.

    Using NSURLConnection

    Apple includes an example of how to use NSURLConnection in URL Loading System Programming Guide, Using NSURLConnection. It’s a short article, full of simple code examples. You should spend a few minutes to read this.

    In a nutshell, here’s the pattern:

    • Keep a NSMutableData for your response data.
    • Clear the contents NSMutableData instance in didReceiveResponse. (You may receive multiple didReceiveResponse events as you’re redirected, but you should only use data from the last one.)
    • Append the data you receive from didReceiveData to the NSMutableData. Don’t try to process it immediately; you will usually receive multiple didReceiveData events for a single transfer.
    • In connectionDidFinishLoading, your data is complete. Here, you can do something with it.

    When you have all the data, you can either send it to a different thread or (much simpler) dispatch_async to a queue that isn’t running on the main thread. See Listing 5, “Example connectionDidFinishLoading: implementation” of URL Loading System Programming Guide.

    The idea here is that your start (or restart) accumulating data in didReceiveResponse:, append data in your didReceiveData:, and actually do something with the data in connectionDidFinishLoading:.

    It is possible to run an NSURLConnection on another thread, of course. That thread will need to be using a run loop to receive the delegate events from networking. But unless there’s some reason you need a separate thread, using asynchronous networking is Apple’s solution to this.

    Using NSURLConnection requires a class member to accumulate data as its transferred. That means that if you’re going to have multiple simultaneous transfers, you’ll need something more complicated. Probably a wrapper class to drive NSURLConnection, and keep each response separate. By the time you’ve written this wrapper class, you’ve probably written a naive version of AFHTTPRequestOperation, a part of AFNetworking.

    Assuming you only have a single transfer going on at once, the code looks a bit like this:

    - (void)signUpUsingEmail:(NSString *)email firstName:(NSString *)firstName
                    lastName:(NSString *)lastName andPassword:(NSString *)password {
    
        // set up your connection here, but don't start it yet.
    
        receivedData = [[NSMutableData alloc] init]; // receivedData should be a class member
        [signUpConnection start];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        [receivedData setLength:0];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [receivedData appendData:data];
    }
    
    - (void)connection:(NSURLConnection *)connection
      didFailWithError:(NSError *)error {
        // do something with error here
        // if you're not using ARC, release connection & receivedData here
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        dispatch_async(someQueue, ^{
            // do something that takes a long time with receivedData here
            dispatch_async( dispatch_get_main_queue(), ^{
                // go back to your main screen here
            });
        });
        // if you're not using ARC, release connection & receivedData here
    }
    

    Using AFNetworking

    Now that I’ve explained all this, I’m going to suggest an alternative. You might be better off using AFNetworking.

    AFNetworking is an open source third party library. Its most basic classes wrap NSURLConnection in a NSOperation (thus, something that can be added to an NSOperationQueue). AFNetworking handles these events I’ve talked about automatically. Instead, you write a completion block. When you get this block, the transfer has either succeeded or failed. If it’s failed, the error is available on the AFHTTPRequestOperation instance. If it’s succeeded, you can use the data on the AFHTTPRequestOperation instance.

    (Note: I believe AFHTTPRequestOperation actually does run the connection from another thread. However, it’s well-written and well-tested code. There’s nothing “wrong” with doing this, it’s just harder to pull off and usually unnecessary. But if your library does it for you, why not?)

    AFNetworking provides some HTTP logic that NSURLConnection doesn’t. With AFNetworking, you’d just use a AFHTTPRequestOperation and set a completion block. All of the above becomes something like:

    HTTPOperation = [[AFHTTPRequestOperation alloc] initWithRequest: URLRequest];
    HTTPOperation.completionBlock = ^{
        dispatch_async(someQueue, ^{
            // get possible error or content from HTTPOperation
        }
    };
    [HTTPOperation start];
    

    In the past, I’ve written using NSURLConnection directly. But recently, I’ve been using AFNetworking more frequently. Unlike some other libraries, it’s not a radically different shape than NSURLConnection. In fact, it uses NSURLConnection, just wrapped in a NSOperation (which hopefully Apple will get to any release now). It’s worth considering, at least.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a public gist via javascript. I'm not using any authentication
I am trying to create a WCF service that uses certificate authentication over SSL
I'm trying to build a safe user authentication system. The code is from http://net.tutsplus.com/tutorials/php/simple-techniques-to-lock-down-your-website/
I have an MVC 3 app I am trying to create some custom authentication
I'm currently trying to create user authorization that follows: The definitive guide to form-based
I am trying to create a custom authentication scheme in ASP.NET MVC using form
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
Trying to create a PhoneGap app using the canvas tag on an iPad. The
I'm super new to Ruby on Rails. I'm trying to make an authentication system
I'm trying to create a page that displays a list of employees. All the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.