Make sure the file 01-a.html is copied to your app bundle. This can be achieved via the Copy Files / Copy bundled resources phase when building the app. The crash indicates, that the file is missing from the bundle and thus a nil value is returned. You should do checks for nil in such cases.
Nil return value from fileURLWithPath
-
07-10-2022 - |
题
I've recently completed an app-project which mainly consisted of html-files in a webView.
Now I set up another project and copied the relevant code-fragments, but the app crashes with this error message:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
this is the code:
NSString *path01 = [[NSBundle mainBundle] pathForResource:@"01-a" ofType:@"html" inDirectory:@"htdocs"];
NSURL *url01 = [NSURL fileURLWithPath:path01];
NSURLRequest *request01 = [NSURLRequest requestWithURL:url01];
[webView01 loadRequest:request01];
I guess the problem is the file-reference, but everything is in its place, but the result is always NIL.
I´ve searched for the problem, it seems there is something wrong with how i imported the files to xcode, but i can´t fix it. Is there a simple trick to get xcode to properly import the folder "htdocs"?
thanks in advance...
解决方案
其他提示
NSURL *url01 = [NSURL fileURLWithPath:path01];
The only parameter is path01
, so let's see where that comes from
NSString *path01 = [[NSBundle mainBundle] pathForResource:@"01-a" ofType:@"html" inDirectory:@"htdocs"];
That means you don't have a file htdocs/01-a.html
in your resources.
Note that xcode flattens resource structure so you probably just want to drop the inDirectory
argument.