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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:54:59+00:00 2026-05-21T02:54:59+00:00

Similar questions have been asked before, but I could never find a solution. Here

  • 0

Similar questions have been asked before, but I could never find a solution.

Here is my situation – my UIWebView loads a remote html page. The images used in the web pages are known at build time. In order to make the page load faster, I want to package the image files in the iOS application and substitue them at runtime.

[Please note that the html is remote. I always get answers for loading both html and image files from local – I have done that already]

The closest recommendation I got was to use a custom url scheme such as myapp://images/img.png in the html page and in the iOS application, intercept the myapp:// URL with NSURLProtocol subclass and replace the image with a local image. Sounded good in theory, but I haven’t come across a complete code example demonstrating this.

I have Java background. I could do this easily for Android using a Custom Content Provider. I am sure a similar solution must exist for iOS/Objective-C. I don’t have enough experience in Objective-C to solve it myself in the short timeframe I have.

Any help will be appreciated.

  • 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-21T02:55:00+00:00Added an answer on May 21, 2026 at 2:55 am

    Ok here is an example how to subclass NSURLProtocol and deliver an image (image1.png) which is already in the bundle. Below is the subclasses’ header, the implementation as well as an example how to use it in a viewController(incomplete code) and a local html file(which can be easily exchanged with a remote one). I’ve called the custom protocol: myapp:// as you can see in the html file at the bottom.

    And thanks for the question! I was asking this myself for quite a long time, the time it took to figure this out was worth every second.

    EDIT:
    If someone has difficulties making my code run under the current iOS version, please have a look at the answer from sjs. When I answered the question it was working though. He’s pointing out some helpful additions and corrected some issues, so give props to him as well.

    This is how it looks in my simulator:

    enter image description here

    MyCustomURLProtocol.h

    @interface MyCustomURLProtocol : NSURLProtocol
    {
        NSURLRequest *request;
    }
    
    @property (nonatomic, retain) NSURLRequest *request;
    
    @end
    

    MyCustomURLProtocol.m

    #import "MyCustomURLProtocol.h"
    
    @implementation MyCustomURLProtocol
    
    @synthesize request;
    
    + (BOOL)canInitWithRequest:(NSURLRequest*)theRequest
    {
        if ([theRequest.URL.scheme caseInsensitiveCompare:@"myapp"] == NSOrderedSame) {
            return YES;
        }
        return NO;
    }
    
    + (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)theRequest
    {
        return theRequest;
    }
    
    - (void)startLoading
    {
        NSLog(@"%@", request.URL);
        NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[request URL] 
                                                            MIMEType:@"image/png" 
                                               expectedContentLength:-1 
                                                    textEncodingName:nil];
    
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"image1" ofType:@"png"];  
        NSData *data = [NSData dataWithContentsOfFile:imagePath];
    
        [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
        [[self client] URLProtocol:self didLoadData:data];
        [[self client] URLProtocolDidFinishLoading:self];
        [response release];
    }
    
    - (void)stopLoading
    {
        NSLog(@"something went wrong!");
    }
    
    @end
    

    MyCustomProtocolViewController.h

    @interface MyCustomProtocolViewController : UIViewController {
        UIWebView *webView;
    }
    
    @property (nonatomic, retain) UIWebView *webView;
    
    @end
    

    MyCustomProtocolViewController.m

    ...
    
    @implementation MyCustomProtocolViewController
    
    @synthesize webView;
    
    - (void)awakeFromNib
    {
        self.webView = [[[UIWebView alloc] initWithFrame:CGRectMake(20, 20, 280, 420)] autorelease];
        [self.view addSubview:webView];
    }
    
    - (void)viewDidLoad
    {   
        // ----> IMPORTANT!!! :) <----
        [NSURLProtocol registerClass:[MyCustomURLProtocol class]];
    
        NSString * localHtmlFilePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"html"];
    
        NSString * localHtmlFileURL = [NSString stringWithFormat:@"file://%@", localHtmlFilePath];
    
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:localHtmlFileURL]]];
    
        NSString *html = [NSString stringWithContentsOfFile:localHtmlFilePath encoding:NSUTF8StringEncoding error:nil]; 
    
        [webView loadHTMLString:html baseURL:nil];
    }
    

    file.html

    <html>
    <body>
        <h1>we are loading a custom protocol</h1>
        <b>image?</b><br/>
        <img src="myapp://image1.png" />
    <body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

similar questions have been asked before but I cant find an exact match to
Similar questions have been asked before, but I can't find the specific details I'm
I know similar questions have been asked before, but I think my solution is
I know similar questions have been asked before, but I can't find one that
I know that similar questions have already been asked here before, but they all
Similar questions have been asked, but nothing exactly like mine, so here goes. We
I know similar questions have been asked before but I haven't seen one where
I appreciate that similar questions have been asked before, but I am struggling to
I know similar questions (regarding cherry-picking) have been asked before, but I haven't really
I know similar questions have been asked before, but still things are not clear

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.