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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:24:04+00:00 2026-05-20T13:24:04+00:00

Im creating application that will display remote page on the UIWebView using local images

  • 0

Im creating application that will display remote page on the UIWebView using local images and javascript/css files for faster loading. I want to avoid using cache because for this to work it must first load all the content. I was searching online for possible solution. Here is what I have:

 NSURLRequest *urlrequest = [ [NSURLRequest alloc] initWithURL: [NSURL URLWithString:urlString] ];
    NSData *returnData = [ NSURLConnection sendSynchronousRequest:urlrequest returningResponse: nil error: nil ];
    NSString *HTMLData = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];


    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    resourcePath = [resourcePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
    resourcePath = [resourcePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

    [webView loadHTMLString:HTMLData baseURL:
    [NSURL URLWithString: 
     [NSString stringWithFormat:@"file:/%@//",resourcePath]
     ]];

urlString is defined and I moved files to the app bundle.

Code works as I can see the remote page but no images nor javascript files are present.

  • 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-20T13:24:05+00:00Added an answer on May 20, 2026 at 1:24 pm

    I solve this problem by inheriting NSURLCache.

    Firstly, create a inheritance of NSURLCache

    #import "VURLCache.h"
    #import <MobileCoreServices/UTType.h>
    
    @implementation VURLCache
    
    -(NSString*)mimeTypeForExtension:(NSString*)ext
    {
        NSAssert( ext, @"Extension cannot be nil" );
        NSString* mimeType = nil;
    
        CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
            (CFStringRef)ext, NULL);
        if( !UTI ) return nil;
    
        CFStringRef registeredType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
        if( !registeredType ) // check for edge case
        {
            if( [ext isEqualToString:@"m4v"] )
                    mimeType = @"video/x-m4v";
            else if( [ext isEqualToString:@"m4p"] )
                    mimeType = @"audio/x-m4p";
            // handle anything else here that you know is not registered
        } else {
            mimeType = NSMakeCollectable(registeredType);
        }
    
        CFRelease(UTI);
        return mimeType;
    }
    
    -(void)parseBundleURL:(NSURL*)url 
                     name:(NSString**)name 
                      ext:(NSString**)ext 
          bundleDirectory:(NSString**)bundleDirectory {
    
        NSString* path = [url path];
        NSUInteger nameStart = NSNotFound;
        // locate the last '/'
        NSRange pathStop = [path rangeOfString:@"/" options:NSBackwardsSearch];
        if( 0 == pathStop.location ) {
            nameStart = 1;
        } else if ( NSNotFound != pathStop.location ) {
            // there is a path
            nameStart = pathStop.location+1;
            NSRange pathRange = NSMakeRange(0, nameStart);
            *bundleDirectory = [path substringWithRange:pathRange];
        }
    
        NSRange fileRange = NSMakeRange(nameStart, path.length - nameStart);
        if( fileRange.length > 0 ) {
            NSRange extStop = [path rangeOfString:@"." options:0 range:fileRange];
            if( NSNotFound != extStop.location ) {
                NSUInteger sep = extStop.location;
                NSRange nameRange = 
                    NSMakeRange( nameStart, sep - nameStart);
                *name = [path substringWithRange:nameRange];
                NSRange extRange = 
                    NSMakeRange( sep+1, path.length -(sep+1));
                *ext = [path substringWithRange:extRange];
            }
        }
    }
    
    -(NSCachedURLResponse*)bundleResourceForRequest:(NSURLRequest *)request {
    
        NSURL* url = [request URL];
        NSString* name = nil;
        NSString* ext = nil;
        NSString* bundleDirectory = nil;
        NSString* path = nil;
    
        [self parseBundleURL:url name:&name ext:&ext bundleDirectory:&bundleDirectory];
    
        if( name && ext ) {
            NSBundle* bundle = [NSBundle mainBundle];
            if( nil == bundleDirectory ) {
                path = [bundle pathForResource:name ofType:ext];
            } else {
                path = [bundle pathForResource:name ofType:ext inDirectory:bundleDirectory];
            }
        }
    
        NSCachedURLResponse* rep = nil;
    
        if( path ) {
            NSData* content = [NSData dataWithContentsOfFile:path];
            NSString* mime = [self mimeTypeForExtension:ext];
            NSString* encoding = nil;
            NSURLResponse* response = 
                [[NSURLResponse alloc] 
                        initWithURL:request.URL 
                           MIMEType:mime 
              expectedContentLength:[content length] 
                   textEncodingName:encoding];
            rep = [[NSCachedURLResponse alloc] initWithResponse:response data:content];
        }
    
        return rep;
    }
    
    #pragma mark NSURLCache
    
    -(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
        NSURL* url = [request URL];
        if( [[url absoluteString] hasPrefix:VBUNDLE_URL_PREFIX] ) {
            return [self bundleResourceForRequest:request];
        }
        return [super cachedResponseForRequest: request];   
    }
    
    @end
    

    Then, replace the default NSURLCache in app delegate

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docDir = [paths objectAtIndex:0];
    NSString *path =  docDir; // the path to the cache file
    NSUInteger discCapacity = 0;
    NSUInteger memoryCapacity = 5120*1024;
    
    VURLCache *cache = [[VURLCache alloc] initWithMemoryCapacity:memoryCapacity diskCapacity:discCapacity diskPath:path];
    [NSURLCache setSharedURLCache:cache];
    [cache release];
    

    Then, you could load file in your app bundle.

    For example,

    That will show your app icon.
    You could also load css with it.

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

Sidebar

Related Questions

I am creating a web application that will display the dates of various events
I'm creating a flash application that will post images to a url for saving
I'm creating a splash screen that will display while my Android application loads. I'd
I'm creating a application that will fetch data from an SQLite database and display
I'm creating an application that will store a hierarchical collection of items in an
I am creating a small application that will be deployed on Window. The database
I am creating an application in .NET that will serve as a second UI
I'm currently creating an application for a customer that will allow them to automatically
I'm creating a Windows Phone 7 application that will consume an exposed webservice. For
I'm creating an application that'll display a random picture based upon a defined letter

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.