¿Cómo colocar un archivo en el directorio de recursos de mi paquete?

I have a .html file that needs to go into my iPhone app's bundle resource directory. I tried FIle -> New -> Resource but nothing seemed to fit the bill.

Right now I'm using a UIWebView loadRequest and have the "pathForResource:@"help.htm"... it crashes when it can't find help.htm. If I put this in a Resource directoyr, I believe I can still use the same code without any directory qualifiers.

¿Cómo hago esto?


Here is the UIWebView code:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if([[segue identifier] isEqualToString:@"helpSegue"])  {

        [slWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"iHelp.htm" ofType:@"html"]isDirectory:NO]]];

        if ([[segue identifier] isEqualToString:@"reportSegue"]) {

Aquí está el error que recibo:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:isDirectory:]: nil string parameter'

preguntado el 09 de marzo de 12 a las 15:03

Can you please post all of your UIWebView code? -

Aren't all files automatically copied to the Resources directory if they aren't source files? -

Updated original post again... -

1 Respuestas

Creo que este es tu problema:

[slWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"iHelp.htm" ofType:@"html"]isDirectory:NO]]];

You need to omit the extension from the file name since it is specified in a separate parameter:

[slWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"iHelp" ofType:@"htm"]isDirectory:NO]]];

Then just add the "iHelp.htm" file to the project as normal making sure it is marked as included the applications target.

Just to be sure, try right clicking your app in the "Products" group and select "Reveal in Finder". Then right click the .app bundle (which is just a folder) and select reveal contents to see what's actually included. If you don't see your "iHelp.htm" file it means it is not marked as a member of the target. Find and select the "iHelp.htm" file in Xcode then open the utilities view. Look under the "Target Membership section and make sure the checkbox for your application target is checked.

Noticias What you see with the above change is this:

[[NSBundle mainBundle] pathForResource:@"iHelp" ofType:@"htm"]

Causes a search in the app bundle for "iHelp.htm" which is returning the path of the htm file where before it was returning nil since it couldn't find it. (Before you were looking for "iHelp.htm.html" which didn't exist. The next part of your problem is figuring out why this path is not properly converted to a URLRequest. You have a lot of inlining, which I personally love, but to correctly debug you may want to break it out into separate variables temporarily.

NSString *iHelpPath = [[NSBundle mainBundle] pathForResource:@"iHelp" ofType:@"htm"];
NSLog(@"iHelpPath = %@", iHelpPath);
NSURL *fileURL = [NSURL fileURLWithPath:iHelpPath];
NSLog(@"fileURL = %@", fileURL);
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
NSLog(@"request = %@", request);

And see what that gives you.

respondido 09 mar '12, 17:03

I did as you suggested, removing the .htm, and changing the parameter to htm, and at least it didn't crash... however, it didn't display the file either. Changed the parameter back to "html" and it crashed. How do I mark the file as "included in the applications target? (btw, Location is set to "Relative to Group") - SpokaneDude

It looks like [[NSBundle mainBundle] pathForResource:@"iHelp" ofType:@"htm"] is finding your resource which is why it no longer crashes. Let me post another answer to explain what's going on. - acantilado

Can't post all of the code here anymore... look here: pastebin.com/d4REAbKL - SpokaneDude

ok it looks like everything is fine as far as constructing the URL request. Try to "open /Users/rolfmarsh/Library/Application Support/iPhone Simulator/5.0/Applications/C45F75FD-9199-4A62-B51F-1CC0C62E3135/SurveyorLeveling-I.app/iHelp.htm" from the cmd line and see if Safari loads it ok. - acantilado

One other thing to understand is that loading URL requests are asynchronous operations. You probably need to register a callback or listener to know when it is finished loading. - acantilado

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.