Question

I want to keep a file in memory and feed it into a lib that works with an URL (think NSURL). It would be nice if I could create some memory mapped file that iOS could access using file:// or similar.

My fallback method is to create a local server socket that serves the file from memory, but I'd like to find a neater way if possible.

Was it helpful?

Solution 2

I ended up using NSURLProtocol to create a new URL-scheme that serves files from memory.

OTHER TIPS

I use this to read the file from the web and cache in memory:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    NSCachedURLResponse *memOnlyCachedResponse =
        [[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response
                                                 data:cachedResponse.data
                                             userInfo:cachedResponse.userInfo
                                        storagePolicy:NSURLCacheStorageAllowedInMemoryOnly];
    return [memOnlyCachedResponse autorelease];
}

That is the connection:willCacheResponse: method from NSURLConnection. My NSURLs point to the web, but should work if you point to a file in your bundle. I got that code from https://github.com/rs/SDURLCache

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top