0
votes

I have a webview which i want to load using the loadHtmlString method. The problem is that I want to be able to change the img src's with images that i have previously downloaded. I also use google analitics in the html so I need to set the baseUrl to the actual url so it will work. Here comes the problem. If I put the baseUrl in, the images will not load. If I don't set the baseUrl, it works. How can I get around this, so I will be able to both use google analitycs and have the images store locally in my application? I would prefer not having to implement the google analitics sdk in my project.

A strange thing is that if I run it in simulator, and not put the "http://" prefix in front of my baseUrl, it works fine. However, when I run it on a device, I receive the following error and it doesn't work:

Domain=WebKitErrorDomain Code=101 "The URL can’t be shown"

Thanks

EDIT

If I do this, it works:

[appsWebView loadHTMLString:htmlString baseURL:nil];

However, I must provide a baseURL in order to have Google Analitics working, I have two further cases:

  1. This one gives the above mentioned error: (it works ok in simulator but gives error when running on device)

    [appsWebView loadHTMLString:htmlString baseURL:[NSURL urlWithString:@"test.com"]];

  2. This one simply doesn't show anything: (neither loads the html string or the url)

    [appsWebView loadHTMLString:htmlString baseURL:[NSURL urlWithString:@"http://test.com"]];

2
Can you paste here the URL and the code you use to load the string? - Fran Sevillano
I can't share the url or the html. However I must say that it works on any browser and also in a webview if I simply load the url or the html string but using the remote images. However, I need them to be stored locally. I can also confirm that the images are stored properly and that when replacing the the images url to the local one, it is correct and the file exists at that location - Andrei Filip

2 Answers

1
votes

I incorrectly assumed that the problem was that the local image was not fully specifying the full path, but that does not appear to be the problem here. But, you are quite right that it appears (somewhat surprisingly) that you cannot specify some web-based baseURL and also reference a local image in your HTML string. No simple solutions are leaping out at me, but at the very least, it appears that you might have a couple of (not very good) options:

First, you could base64 encode the local image using some base64 library like Mike Gallagher's NSData+Base64 category, e.g.:

NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSString *imageDataBase64 = [imageData base64EncodedString];
NSString *imageHtml = [NSString stringWithFormat:@"<img src='data:image/png;base64,%@'>", imageDataBase64];

This slows the initial rendering, but maybe it's better than nothing.

Second, you could always try leaving the baseURL as nil, removing the JavaScript that does the Google Analytics from the HTML string, and then try injecting that JavaScript via stringByEvaluatingJavaScriptFromString. This approach may or may not work depending upon the complexity of the Google Analytics JavaScript (e.g. what further web-based references it might have), but there's a outside chance you might be able to do something that way.

My apologies for assuming the problem was a trivial img URL. Clearly you had identified a more fundamental issue.


Original answer:

Create your image URLs in your HTML string to be fully qualified file URLs within your local file system:

The image is either in Documents:

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:imageName];

Or in the bundle:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName 
                                                      ofType:nil];

But, once you have fully qualified path, you should be able to use that:

NSURL *imageUrl = [NSURL fileURLWithPath:imagePath];
NSString *imageHtml = [NSString stringWithFormat:@"<img src='%@'>", imageUrl];
0
votes

I would bet it's a casing issue. Take into account that the Device is case sensitive whereas the Simulator is not. Check the URL and make sure it contains the right characters.