iPhone UiWebView images with absolute path - iphone

I display some html in my UIWebView and I have in my html references to images like: '/images/test1.gif' etc.
I display this in that way:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[myWebView loadHTMLString:someHtml baseURL:baseURL];
I've also added all required images to my Xcode project, into 'images' directory. The problem is, that, as far as I can see, the baseUrl ends with '/', and path to the images starts with '/' (it's absolute). All that makes my images doesn't appear.
I've noticed, that if I change, in html, path to images, to for example 'images/test.gif', the image will show up, but I want (if possible) to avoid changing all image paths in html from absolute to relative, becasue this html is imported from some database, and in case of another import I would have to change it again.

Have you tried clipping the slash from the end of baseURL? I should hope the web view is smart enough to add a slash where needed, despite blindly concatenating two strings that result in a double slash.

Related

HTML file creation with image

I created an HTML file programmatically with images sources. The images are stored in the documents directory. The problem is that when I give the image source path as document directory path the images are not shown in Windows.
So, I choose the second option is to give only the image's name instead of whole path. Then it work well on Mac & Windows, but now images are not shown on the device's UIWebView.
Then I selected to open the HTML file in Safari insted of UIWebView, but the safari could not be opened because URL becomes NULL.
My code to open Safari is as follow:
NSLog(#"%#",documentsDirectory);
NSString *urlStr = [NSString stringWithFormat:#"%#/AccReport1.html",documentsDirectory];
NSLog(#"urlStr :: %#",urlStr);
NSURL *url1=[[NSURL alloc]init ];
url1 = [NSURL fileURLWithPath:urlStr];
NSLog(#"url :: %#",url1);
[[UIApplication sharedApplication]openURL:url1];
I want to mail the HTML file that contains images. And these HTML file and images are stored in document directory. I want to show this HTML file with images in windows browser, mac browser and iphone's safari.
How to do this?
One of the simple way to solve your problem is to keep Images and HTML file in same directory and refer those images in HTML files just by name and if you put them in a folder then give the complete path.
The images are not showing because the image path specified within the HTML code is not the documents directory path.
I suggest you might have to construct an html string and then subject the uiwebview to load that string
NSString *htmlString = [NSString stringWithFormat:#"<html><body><img src=\" %#/image.png \"></img></body></html>",documentsDirectoryPath];
[webView loadHTMLString:htmlString baseURL:nil];

UIWebView loadHTMLString not working in iOS5

Perhaps this is similar to this question, which has no responses: loadHTMLString Not Working With iOS5?
I have a UIWebView which I populate using loadHTMLString:baseURL:. The HTML is small and simple, and it references a css style sheet and a javascript script, which are loaded via the baseURL:, which is set to a directory inside the app's bundle.
// load the html
NSString* filePath = [NSString stringWithFormat: #"%#/html", [[NSBundle mainBundle] resourcePath ] ];
[_pCurrentWebView loadHTMLString: html baseURL: [NSURL fileURLWithPath: filePath isDirectory: YES ] ];
This has always worked in the past, but it is broke in iOS5. In iOS5, nothing is displayed in the UIWebView. The webview does source all of the expected events - e.g. shouldLoadRequest, didStartLoad, didFinishLoad, etc.
The html has a script tag, like this:
<script type="text/javascript" src="./myscript.js" />
If I remove the script tag then the page loads and renders fine in iOS5. And I can tell that the css file, which is referenced the same way as the script .js file, is loaded and applied.
If I keep the script tag but make the myscript.js file completely empty it still fails to load.
To me, this seems like some sort of cross-site-scripting issue - in that the WebView thinks that it should disallow loading the script (and in fact, disallow rendering of the page??)
Not sure where to go from here. Ideas?
UPDATE
This is feeling more and more like a cross-site-scripting issue. If I remove the tag it works, albeit sans script. All my images are loaded from the baseURL, as is my stylesheet. That is, we know the baseURL is working.
If I replace the tag with the actual contents of my script file then it works, so the problem is not the script itself.
Still looking for confirmation and additional ideas to circumvent. It's inconvenient for me to have to patch in the script itself into the html, but this is my best solution thus far. Alternatively I could write the html to the filesystem and load via loadRequest, but again, not my first choice.
UPDATE 2
Thanks to #djromero I have a solution. My document is a XHTML document and as such used a self-closing script tag (no content, just attributes.) But loadHTMLString:baseURL: apparently assumes a MIMEType of text/html, which the UIWebView apparently now interprets more strictly - and in text/html documents you may not have self closing tags.
My solution is to switch to loadData:MIMEtype:baseURL: and specify application/xhtml+xml as the mime type. I can easily construct the NSData from my NSString using dataUsingEncoding:.
I'm not an HTML standards expert, but...did you try to close the <script> tag:
<script type="text/javascript" src="./myscript.js"></script>
It worked for me.
The way to load an HTML file that contains embedded folder references, such as '/file.js', is to load it as a URL rather than as a STRING.
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"htm"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
I use this along with referenced folders (not referenced files) to create an ordinary website structure in Xcode, with js/ and css/ and images/ references in the embedded index.htm file, e.g.
<script type="text/javascript" src="js/jquery-1.6.4.min.js"></script>
UPDATE
I don't think that referencing a URI within a loaded HTML string was officially supported. If you must use a string, then load the needed resource files into the string before you load it into the UIWebView.

display local html file with css in iphone..?

I want to display local html file that has css too in UIWebView. I am able to do that using following code..
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"info" ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:path];
[webView loadHTMLString:htmlString baseURL:baseUrl];
I am not able to get hover effects and my understanding is my be there is no hover kind of thing in a touch mobile,
Any ways my main issue is I had deleted my index.html from resources and then re added the updated one but not getting the updated one..
I don't know what happen the web view still loads the old html file. I tried to clean project too.
Second thing can I get css effects in iphone..?
Thanks..
As far as I know you can only achieve this result by using javascript-events like ontouchstart (or something).
I believe if you reset your simulator (While running your simulator, go to iOS Simulator > Reset Content and Settings in the menu bar) then re-run your app, the UIWebView will load the new index.html. UIWebViews cache these things, so you can add code to clear that cache and/or ignore the cache. Check here: Clearing UIWebview cache

UIWebView locally save images and just load html

i want to load just the html from the web and show the images from the local resource.
This helped me a lot, but is the path to the resource on every device the same? Or does it depend on an installation path or similar?
Load Content in the UIWebView on the Iphone
the path to resources is different on every device, that's why you use the function
[[NSBundle mainBundle] pathForResource:nameOfImage ofType:#"jpg"]
the html loading from the web you can do all kinds of ways. The easiest is just:
NSString *myHTML = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://web.site.com/file.html"]];

UIWebView display locally stored website (HTML, images, Javascript)

I've looked EVERYWHERE for this, can't find anything. I basically need to store an entire website inside of my iPhone for some interesting reasons. Regardless I can't figure out how to display it correctly in my UIWebView.
EDIT: I should clarify, I can load the original HTML file, and I have chagned all of the pathing to be local, except nothing gets linked in.
Here is the code
self.dataDetectorTypes = UIDataDetectorTypeLink;
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self loadRequest:request];
index.html has a bunch of <script type="text/javascript" src="somescript.js">
None of the JS code gets executed
Looks like you're loading the HTML from inside your bundle. This means that all the additional files (.js, .css, and any media files) also need to be present in your bundle. So the first thing to check is to look inside the contents of your executable and make sure the js, etc. files are included.
If that looks fine the next thing to check is if the html, js, or css files reference content via relative or absolute URLs. If there's an absolute path reference in the web content then UIWebView is going to try to download that content each time so it'll only work when you have a net connection. If the path is relative then it's going to look in the bundle to see if such a file exists.
When you included the html and content into the XCode project file you probably dragged the file(s) over to the project side-bar and were asked whether to "Recursively create groups for any added folders" or to "Create Folder References for any added folders."
The default is the first one which means XCode creates a yellow folder in your project, but it'll ignore the directory hierarchy on disk when time comes to generate the output bundle. If you choose the second option then the folder is blue and if you look in your output bundle you'll see that the whole folder hierarchy has been replicated.
The first works for simple web pages where everything is at the same folder level and you can use the method you list above to load it. The second case works better if your web page is complex and references content in sub-folders in which case you need to load the web pages from a relative path (say, the 'webpages' folder):
NSString *path = [[NSBundle mainBundle]
pathForResource:#"index" ofType:#"html"
inDirectory:#"webpages"];
The last thing to check for is if there are any BASE tags in the html file. This is a way to specify a default address or target for all links on a page, but it can muck up webview links.
The problem is that this call:
NSURL *url = [NSURL fileURLWithPath:path];
doesn't setup a baseURL and so relative paths in the .html file for things like javascript, css, images etc don't work.
Instead use this:
url = [NSURL URLWithString: [path lastPathComponent]
relativeToURL: [NSURL fileURLWithPath: [path stringByDeletingLastPathComponent]
isDirectory: YES]];
and then things like "styles.css" in the index.html file will be found IFF they are copied into the bundle next to the .html file.
You need to set this:
myWebView.dataDetectorTypes = UIDataDetectorTypeLink
Make sure that the .js files are in your copy to resource bundle section and not in the compile section. Xcode places them in the compile group by default.
When adding pathFor resource in Dictionary , it displays a nil string error.
My attempt was to run an entire web page out of the Xcode project file. To do that you must:
When importing the file select "Create folder references for any added folders".
Set up the web view, but make sure you set the relative path as previously mentioned.
NSString *path = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"html"
inDirectory:#"Directory"];
NSURL *url = [NSURL URLWithString:[path lastPathComponent] relativeToURL:
[NSURL fileURLWithPath: [path stringByDeletingLastPathComponent]
isDirectory:YES]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.currentWebView loadRequest:request];