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

  • Home
  • SEARCH
  • 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 4058224
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:03:09+00:00 2026-05-20T15:03:09+00:00

I have a basic question and I am hoping there is a simple answer

  • 0

I have a basic question and I am hoping there is a simple answer that I have missed. The basic premise is that I am going to connect to a website and download some JSON data. I am using the framework provided by Stig Brautaset and the tutorial worked really well.

My issue is that the website I am connecting to is in the format and the username and password are fixed for my app so the user will never be entering them.

curl -u username:password http://website.com/page

How do I pass in the url and password to a NSURLConnection?

It doesn’t have to be an NSURLConnection, it just seems the best option.

I have looked at the AdvancedUrlConnections sample but it seems overly complex and is quite old. Searching on the web hasn’t been much better. I am hoping that one of you guys can say just set these 2 properties, followed by suitable insults of course…

  • 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-05-20T15:03:10+00:00Added an answer on May 20, 2026 at 3:03 pm

    An NSURLConnection will work just fine. As pointed out in another answer, implementing the didReceiveAuthenticationChallenge callback is quite easy to do.

    - (void) someMethod
    {
        NSURLRequest* request = [[NSURLRequest alloc] 
             initWithURL:[NSURL urlWithString:@"someURL"]
    
        NSURLConnection* connection = [[NSURLConnection alloc] 
             initWithRequest:request delegate:self];
    
        [connection release];
        [request release];
    }
    

    This is all pretty straight forward. It’s in the delegate methods of the NSURLConnection where all the magic happens:

    This is where you handle the challenge for credentials. I have hardcoded a fake username and password to demonstrate how it works. I personally have a separate delegate object handle the challenge. Keep in mind that the connection will sit in an idle state until you respond to it or until the connection times out.

    - (void) connection:(NSURLConnection *)connection 
          didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
        //  Make sure to use the appropriate authentication method for the server to
        //  which you are connecting.
        if ([[challenge protectionSpace] authenticationMethod] == 
                 NSURLAuthenticationMethodBasicAuth)
        {
                //  This is very, very important to check.  Depending on how your 
                //  security policies are setup, you could lock your user out of his 
                //  or her account by trying to use the wrong credentials too many 
                //  times in a row.
            if ([challenge previousFailureCount] > 0)
            {
                [[challenge sender] cancelAuthenticationChallenge:challenge];
    
                UIAlertView* alert = [[UIAlertView alloc] 
                                initWithTitle:@"Invalid Credentials" 
                                      message:@"The credentials are invalid." 
                                     delegate:nil 
                            cancelButtonTitle:@"OK" 
                            otherButtonTitles:nil];
                [alert show];
                [alert release];      
            }
            else
            {
                [challenge useCredential:[NSURLCredential 
                       credentialWithUser:@"someUser" 
                                 password:@"somePassword" 
                              persistence:NSURLCredentialPersistenceForSession 
               forAuthenticationChallenge:challenge]];
            }
        }
        else
        {
            //  Do whatever you want here, for educational purposes, 
                //  I'm just going to cancel the challenge
            [[challenge sender] cancelAuthenticationChallenge:challenge];
        }
    }
    

    You’ll need to implement these other methods for an NSURLConnection, as well:

    //  So you know when it's done downloading
    - (void) connectionDidFinishLoading:(NSURLConnection *)connection;
    //  In case of failure
    - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;    
    //  Gather the downloaded file data
    - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a really basic question but... I have some code like this var
Another basic Rails question: I have a database table that needs to contain references
I have some questions about basic CSS that I was unable to understand or
Sorry for the basic question - I'm a .NET developer and don't have much
Dup of Some Basic PHP Questions Hello, I have a heap of tinyint fields
I have a basic form with controls that are databound to an object implementing
I have a basic CRUD form that uses PageMethods to update the user details,
I have a basic model in which i have specified some of the fields
Currently, i have basic C++ and PHP skills. But, i want to switch to
I have a basic understanding of mock and fake objects, but I'm not sure

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.