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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:45:35+00:00 2026-06-04T00:45:35+00:00

For a (web)application I am creating, I need to use Basic Authentication to load

  • 0

For a (web)application I am creating, I need to use Basic Authentication to load pages in my UIWebView.

Now to set the authorization header I use:

NSString *result = [NSString stringWithFormat:@"%@:%@", username, password];
NSData *resultData = [result dataUsingEncoding:NSASCIIStringEncoding];

NSString *result64 = [NSString stringWithFormat:@"Basic %@", [resultData base64Encoding]];
[request setValue:result64 forHTTPHeaderField:@"Authorization"];

In my apache access logs I can see the login is a success. But then the UIWebView wants to load the resources like style.css and jquery.min.js but requests to these resources fail because they have no Authorization header set. How can I fix this?

  • 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-04T00:45:37+00:00Added an answer on June 4, 2026 at 12:45 am

    The solution was to subclass NSURLProtocol to authenticate the resource loading:

    #import <Foundation/Foundation.h>
    
    #import "AuthenticationUtils.h"
    
    @interface CustomURLProtocol : NSURLProtocol <NSURLConnectionDelegate>
    {
        NSMutableURLRequest *_customRequest;
        NSURLConnection *_connection;
    }
    
    @end
    
    @implementation CustomURLProtocol
    
    static NSString *AUTHORIZED_REQUEST_HEADER = @"X-AUTHORIZED";
    
    +(BOOL) canInitWithRequest:(NSMutableURLRequest *)request
    {
        // check if the request is one you want to authorize
        BOOL canInit = (![request.URL.scheme isEqualToString:@"file"] && [request valueForHTTPHeaderField:[AUTHORIZED_REQUEST_HEADER stringByAppendingString:[request.URL absoluteString]]] == nil);
        return canInit;
    }
    
    -(id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id<NSURLProtocolClient>)client
    {        
        _customRequest = [request mutableCopy];
        [_customRequest setValue:@"" forHTTPHeaderField:[AUTHORIZED_REQUEST_HEADER stringByAppendingString:[request.URL absoluteString]]];
    
        self = [super initWithRequest:_customRequest cachedResponse:cachedResponse client:client];
    
        return self;
    }
    
    +(NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request
    {
        NSMutableURLRequest *customRequest = [request mutableCopy];
        [customRequest setValue:@"" forHTTPHeaderField:[AUTHORIZED_REQUEST_HEADER stringByAppendingString:[request.URL absoluteString]]];
    
        NSString *basicAuthentication = [AuthenticationUtils getBasicAuthentication];
        [customRequest setValue:basicAuthentication forHTTPHeaderField:@"Authorization"];
    
        return customRequest;
    }
    
    - (void) startLoading
    {
        NSString *basicAuthentication = [AuthenticationUtils getBasicAuthentication];
        [_customRequest setValue:basicAuthentication forHTTPHeaderField:@"Authorization"];
    
        _connection = [NSURLConnection connectionWithRequest:_customRequest delegate:self];
    }
    
    - (void) stopLoading
    {
        [_connection cancel];
    }
    
    #pragma mark - NSURLConnectionDelegate
    
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
    {
        // This protocol forgets to store cookies, so do it manually
        if([redirectResponse isKindOfClass:[NSHTTPURLResponse class]])
        {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:[(NSHTTPURLResponse*)redirectResponse allHeaderFields] forURL:[redirectResponse URL]] forURL:[redirectResponse URL] mainDocumentURL:[request mainDocumentURL]];
        }
    
        [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [[self client] URLProtocol:self didLoadData:data];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
    {
        [[self client] URLProtocol:self didFailWithError:error];
    }
    
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection 
    {
        [[self client] URLProtocolDidFinishLoading:self];
    }
    
    -(NSURLRequest *) connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
    {    
        // This protocol forgets to store cookies, so do it manually
        if([redirectResponse isKindOfClass:[NSHTTPURLResponse class]])
        {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:[(NSHTTPURLResponse*)redirectResponse allHeaderFields] forURL:[redirectResponse URL]] forURL:[redirectResponse URL] mainDocumentURL:[request mainDocumentURL]];
        }
    
        // copy all headers to the new request
        NSMutableURLRequest *redirect = [request mutableCopy];
        for (NSString *header in [request allHTTPHeaderFields]) 
        {
            [redirect setValue:[[request allHTTPHeaderFields] objectForKey:header] forHTTPHeaderField:header];
        }
    
        return redirect;
    }
    
    @end
    

    And in your AppDelegate add this:

    [NSURLProtocol registerClass:[CustomURLProtocol class]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a web application in which teachers need to be able to easily
I am creating a web application and I want to use the Tabs widget
I am creating a Web application using ASP.NET MVC, and I'm trying to use
I'm creating a new web application which will use a bunch of Data Access
I am creating quite a complex web application. I like to use php to
We've got a Spring based web application that makes use of Hibernate to load/store
I am going to be creating a web application for internal company use. I
All, I am creating a web application and I need to give users the
I'm developing a web application and I need to mix Forms & Windows authentication
I want to do this web application I am creating in the best possible

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.