Search code examples
objective-cxcodefilepathnsfilemanager

check if file exists in project folder


My project has this structure:

my_project
 my_project
  AppDelegate.m
  AppDelegate.h
  ...
  Audio_folder
   my_audio.mp3

So I want to check if an audio file exists or not in the Audio_folder. I can do it with the full path

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:@"/Users/user/Desktop/my_project/my_project/Audio_folder/my_audio.mp3"];

but how can I achieve it with something like

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:@"Audio/my_audio.mp3"];

?


Solution

  • The only way to know whether this is available or not at runtime is by using NSBundle.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"my_audio" ofType:@"mp3"];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
    

    In fileExists you know if it is available.

    Be sure to check the build-phases if the file is copied to the bundle.