I’m developing application which shows page and images from app resources in webview. The page works fine, but the images aren’t there when on iOS 5.1. When I’m testing it on iOS 4.3 or iOS 6.0 everything works like a charm; images are being shown properly. It seems like something’s wrong with that exact system version. Here’s the code I am using:
NSString *imgPath = [[NSBundle mainBundle] pathForResource:currentImageName ofType:@"png"];
imgPath = [NSString stringWithFormat:@"%@%@",@"file://",imgPath];
content = [content stringByReplacingOccurrencesOfString: @"{}" withString:imgPath];
myData = [content dataUsingEncoding:NSUTF8StringEncoding];
[webView loadData:myData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
Content is the string with html. myData is NSData with html file. Image path is generated properly. As I mentioned before – everything works fine on iOS 4.3 and 6.0.
Here are the values of the data on iOS 5.1 emulator:
imgPath = file:///Users/deimos/Library/Application Support/iPhone Simulator/5.1/Applications/98C3911D-46D6-49B6-BADC-6B463619EF5E/myApplication.app/pl.lproj/image01.png
content before replacing the image url:
<body>
<center><img src="{}" /></center>
</body>
content after replacing the image url:
<body>
<center><img src="file:///Users/deimos/Library/Application Support/iPhone Simulator/5.1/Applications/98C3911D-46D6-49B6-BADC-6B463619EF5E/myApplication.app/pl.lproj/image01.png" /></center>
</body>
I’ve found solution to my problem. In the line:
passing empty string to this method is wrong. It should be replaced by nil:
With nil everything works fine on every system version.