My app is an eReader for viewing books on iPhone/iPad. User has an option of downloading the book which we save on device as an .epub file (zipped content) - this enables the book viewing when offline. The page content is in html.
Some assets, like videos, are not in this .epub content though. When user navigates to such a page I have to detect the external asset which is not in .ePub and display a simple placeholder with text "This content is unavailable when offline".
These videos could be embedded in an iFrame html tag or an object tag.
Right now we have a class which inherits from NSURLProtocol abstract class and I am using it to handle this requirement. I have implementation for these 3 methods:
+ (BOOL) canInitWithRequest:(NSURLRequest *)request
- (void)startLoading
- (void) sendResponse:(NSData *)data mimetype:(NSString *)mimetype url:(NSURL *)url
My problem right now is that I have a simple .png that shows "This content is unavailable when offline" but it’s ugly because sometime the area of the iFrame is smaller than the image. How do I scale it to fit? Or is there a better approach? It doesn’t have to be an image as long as I can show this message that content is not available. My current code in startLoading method:
- (void) startLoading
{
…
NSString *bundlePath = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] bundlePath],@"PxeReaderResources.bundle"];
NSString *pathToPlaceholder = [NSString stringWithFormat:@"%@/%@",bundlePath,@"OfflinePlaceholder.png"]; //my placeholder image
NSLog(@"Path to placeholder is: %@", pathToPlaceholder);
data = [[NSFileManager defaultManager] contentsAtPath:pathToPlaceholder];
…
[self sendResponse:data mimetype:@"application/octet-stream" url:self.request.URL];
}
- (void) sendResponse:(NSData *)data mimetype:(NSString *)mimetype url:(NSURL *)url
{
NSDictionary *headers = @{@"Content-Type" : mimetype, @"Access-Control-Allow-Origin" : @"*", @"Cache-control" : @"no-cache"};
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:url statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:headers];
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[self.client URLProtocol:self didLoadData:data];
[self.client URLProtocolDidFinishLoading:self];
}
Thank you for any suggestions.