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 8538663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:10:28+00:00 2026-06-11T11:10:28+00:00

The following code successfully connects to my Ruby on Rails API and returns JSON

  • 0

The following code successfully connects to my Ruby on Rails API and returns JSON using AFNetworking. What do I need to do to edit this to pass in a username and password so my API can use HTTP Basic Authentication?

I’ve read their documentation, but I am new to both Objective-C and AFNetworking and it isn’t currently making sense.

NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:3000/tasks.json"];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation
                                     JSONRequestOperationWithRequest:request
                                     success:^(NSURLRequest *request
                                     , NSHTTPURLResponse *response
                                     , id JSON) {

    self.tasks = [JSON objectForKey:@"results"];
    [self.activityIndicatorView stopAnimating];
    [self.tableView setHidden:NO];
    [self.tableView reloadData];

    NSLog(@"JSON");

} failure:^(NSURLRequest *request
                , NSHTTPURLResponse *response
                , NSError *error
                , id JSON) {
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];

[operation start];
  • 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-11T11:10:29+00:00Added an answer on June 11, 2026 at 11:10 am

    Answer updated for AFNetworking 2.x

    For AFNetworking 2.x:

    In 2.x, they did away with AFHTTPClient, so you’ll need to extend AFHTTPRequestOperationManager with your own class. Then, you can call that class from other code. For example, here’s a sample class that extends the AFHTTPRequestOperationManager:

    SBAPIManager.h:

    #import "AFHTTPRequestOperationManager.h"
    
    @interface SBAPIManager : AFHTTPRequestOperationManager
    
    - (void)setUsername:(NSString *)username andPassword:(NSString *)password;
    
    + (SBAPIManager *)sharedManager;
    
    @end
    

    SBAPIManager.m:

    #import "SBAPIManager.h"
    #import "AFNetworkActivityIndicatorManager.h"
    
    @implementation SBAPIManager
    
    #pragma mark - Methods
    
    - (void)setUsername:(NSString *)username andPassword:(NSString *)password
    {
        [self.requestSerializer clearAuthorizationHeader];
        [self.requestSerializer setAuthorizationHeaderFieldWithUsername:username password:password];
    }
    
    #pragma mark - Initialization
    
    - (id)initWithBaseURL:(NSURL *)url
    {
        self = [super initWithBaseURL:url];
        if(!self)
            return nil;
    
        self.requestSerializer = [AFJSONRequestSerializer serializer];
    
        [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    
        return self;
    }
    
    #pragma mark - Singleton Methods
    
    + (SBAPIManager *)sharedManager
    {
        static dispatch_once_t pred;
        static SBAPIManager *_sharedManager = nil;
    
        dispatch_once(&pred, ^{ _sharedManager = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:3000"]]; }); // You should probably make this a constant somewhere
        return _sharedManager;
    }
    
    @end
    

    Then, in your code, you can call it like this:

    [[SBAPIManager sharedManager] setUsername:yourUsernameVariableHere andPassword:yourPasswordVariableHere];
    
    [[SBAPIManager sharedManager] GET:@"/tasks.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.tasks = [responseObject objectForKey:@"results"];
        [self.activityIndicatorView stopAnimating];
        [self.tableView setHidden:NO];
        [self.tableView reloadData];
    
        NSLog(@"JSON");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // error stuff here
    }];
    

    For AFNetworking 1.x:

    The best practice for this in AFNetworking is to extend the AFHTTPClient with your own class. Then, you can call that class from other code. For example, here’s a sample class that extends the AFHTTPClient:

    SBAPIManager.h:

    #import "AFNetworking/AFHTTPClient.h"
    
    @interface SBAPIManager : AFHTTPClient
    
    - (void)setUsername:(NSString *)username andPassword:(NSString *)password;
    
    + (SBAPIManager *)sharedManager;
    
    @end
    

    SBAPIManager.m:

    #import "SBAPIManager.h"
    #import "AFJSONRequestOperation.h"
    #import "AFNetworkActivityIndicatorManager.h"
    
    @implementation SBAPIManager
    
    #pragma mark - Methods
    
    - (void)setUsername:(NSString *)username andPassword:(NSString *)password
    {
        [self clearAuthorizationHeader];    
        [self setAuthorizationHeaderWithUsername:username password:password];
    }
    
    #pragma mark - Initialization
    
    - (id)initWithBaseURL:(NSURL *)url
    {
        self = [super initWithBaseURL:url];
        if(!self)
            return nil;
    
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
        [self setDefaultHeader:@"Accept" value:@"application/json"];
        [self setParameterEncoding:AFJSONParameterEncoding];
    
        [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    
        return self;
    }
    
    #pragma mark - Singleton Methods
    
    + (SBAPIManager *)sharedManager
    {
        static dispatch_once_t pred;
        static SBAPIManager *_sharedManager = nil;
    
        dispatch_once(&pred, ^{ _sharedManager = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:3000"]]; }); // You should probably make this a constant somewhere
        return _sharedManager;
    }
    
    @end
    

    Then, in your code, you can call it like this:

    [[SBAPIManager sharedManager] setUsername:yourUsernameVariableHere andPassword:yourPasswordVariableHere];
    
    [[SBAPIManager sharedManager] getPath:@"/tasks.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.tasks = [responseObject objectForKey:@"results"];
        [self.activityIndicatorView stopAnimating];
        [self.tableView setHidden:NO];
        [self.tableView reloadData];
    
        NSLog(@"JSON");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // error stuff here
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following code successfully with HTTP but I would like to use
I need to sort nodes in xml. I have the following code which successfully
I am successfully instantiating/automating Visual Studio using the following code: System.Type t = System.Type.GetTypeFromProgID(VisualStudio.DTE.9.0);
Using the following code, I'm able to successfully open a raw disk on my
I am successfully posting a form via ajax, using the following code; $.post( Page.do?source=ajax,
I've written the following code to (successfully) connect to a socks5 proxy. I send
I am building an XML document successfully with the following code: public function build($result)
I am using the following code for sending e-mail (gmail) using Java program. I
This is the following code: `package com.tom.jam; //import java.sql.Connection; //import java.sql.DriverManager; import java.sql.*; import
I am using the following code below: import smtplib import zipfile import tempfile from

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.