0

Context: iOS App written in Obj-C.

I'm trying to load local HTML files which have local images attached to them.

The HTML files are stored in a directory called pages. The images are stored in a directory called images. Here's the folder structure on the project:

/
-/ Images
-/ Pages
Other files

Both "pages" and "images" directories are listed in my project and under Copy Bundle Resources in the Build Phases of my project. When I right click them there, I can Show them in the Finder and see all the individual .html and .jpg files.

I'm having a couple of issues with my code, the main of which is, even though the html pages are loading, the images - which are linked through img tags - are not.

I have tried

Here's the code that loads the HTML page without the images (I'm almost certain that this particular code used to work in the past):

// Linkpath is the path to the HTML file
NSData *htmlData = [NSData dataWithContentsOfFile:linkPath];
if (htmlData) {
    [webWiki loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:linkPath]]];
}

And here's a sample HTML page: http://pastebin.com/b1KTZS6A


Here are some of the solutions I've already tried:

One:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];

NSString* newStr = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
[webWiki loadHTMLString:newStr baseURL:baseURL];

Two

Community
  • 1
  • 1
edoreld
  • 303
  • 1
  • 17

1 Answers1

0

I guess your problem is about types of folders in XCode. Here is the document explain about that types of folders https://developer.apple.com/library/ios/recipes/xcode_help-structure_navigator/articles/Adding_an_Existing_File_or_Folder.html

So the problem is that in Xcode, there are 2 types of folders: groups and folder reference. The groups are yellow ones and the folders references are blue ones. For the local HTML, you have to use as "folder reference" to keep their structure on the actual file system. As a result, your image links will not be broken.

XCode

HTML content

I hope this would be helpful.

HSG
  • 1,224
  • 11
  • 17
  • [This is how it's organised on my side](https://s32.postimg.org/c83benbwl/Screen_Shot_2016_07_15_at_12_04_00.png) – edoreld Jul 15 '16 at 10:40
  • @edoreld is that because of your database folder need to be as folder reference and put into resources folder. As I used to get the full path of an image like this code NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"someImageFile.png" ofType:nil inDirectory:@"FolderA"]; UIImage *image = [UIImage imageWithContentsOfFile:fullPath]; – HSG Jul 15 '16 at 11:02