简体   繁体   中英

File not found on iOS device, but works in Simulator

When I test my app on the simulator, everything works fine. But when I test on my iOS Device, I get this error:

*** Terminating app due to uncaught exception 'FileNotFound', reason: 'file '/Users/name/Documents/appname/GFX/MainMenu.png' not found'

I checked the Build Phases Copy Bundle Resources and everything is added. When I highlight the file in Xcode , Target Membership is checked in utilities.

I also tried omitting the code that loads the file, but I get the same error on a different file. It seems that only the app icons and launch images load properly.

Please note that I am using Sparrow Framework (if that makes any difference). The textures are loaded like so:

SPTexture *gameMenuTexture = [SPTexture textureWithContentsOfFile:(@"/Users/name/Documents/appname/GFX/MainMenu.png")];

SPImage *gameMenuImage = [SPImage imageWithTexture:(gameMenuTexture)];
[self addChild:gameMenuImage];

Help will be much appreciated. I've looked all over forums for answers. Thanks!

Issue is with the hard coded path:

@"/Users/name/Documents/appname/GFX/MainMenu.png"

In device there will not be any folder in the specified path.

If it is on bundle use:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MainMenu" ofType:@"png"];
SPTexture *gameMenuTexture = [SPTexture textureWithContentsOfFile:filePath];

测试像这样的SPTexture *gameMenuTexture = [SPTexture textureWithContentsOfFile:(@"MainMenu.png")];

Your simulator is just an application running on your Mac. It can easily access files on our Mac such as /Users/name/Documents/appname/GFX/MainMenu.png.

Your iOS device cannot directly access files on your Mac. Latest when you distribute your app, the 3rd party users can hardly access anything on your Mac.

Think it over. It does make sence.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM