0

I have a file that is included on the bundle that has the following name:

databaseX.sqlite

where X is the app's version. If the version is 2.8, the file should be named database2.8.sqlite. I have to be sure to include this file when the app is submitted to Apple.

Is it possible to create a compiler directive to check if the file is in the bundle?

I have tried this, without success

#define fileInBundle [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"LoteriaMac%@.sqlite", [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"]]]

#if defined(fileInBundle)
#pragma message("file in bundle")
#else
#pragma message("file missing")
#endif

file in bundle is always shown even if the file is not in bundle.

Duck
  • 34,902
  • 47
  • 248
  • 470

1 Answers1

0

This is not possible. You are trying to use a runtime check inside a compilation directive.

In general, when compiling you cannot know whether there is a file in a bundle or not because the files are usually added to the bundle independently on code, after compiling.

This is the same as checking whether there is a file present in the filesystem on another computer when compiling.

To check that during build time, you can create a custom build script (Build Phases => + button) in your target, something similar to:

APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}"

// there is probably some easier way to get the version than from the Info.plist
INFO_FILE="${APP_PATH}/Info.plist"
VERSION=`/usr/libexec/plistbuddy -c Print:CFBundleShortVersionString "${INFO_FILE}"`

// the file we want to exist
DB_FILE="${APP_PATH}/database${VERSION}.sqlite"

// if the file does not exist
if [ ! -f "${DB_FILE}" ]; then
   // emit an error 
   echo "error: File \"${DB_FILE}\" not found!" >&2;
   // and stop the build
   exit 1
fi
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Since it's before "submission", could this be done in a script at compile? Of course, another one could remove it. – Larme Feb 21 '17 at 21:08
  • @Larme You can do anything in build or post-build scripts. – Sulthan Feb 21 '17 at 21:30
  • @Sulthan - do you know how to do it using a script on the build phases? – Duck Feb 21 '17 at 21:39
  • @SpaceDog I have added a sample script, probably can be simplified or maybe it needs some small changes. In short, it just looks into the app if the file is there. – Sulthan Feb 21 '17 at 22:06
  • when I pressed the (+) there saw 3 options `New copy files phase` and `new run script phase` and `new headers phase`. I suppose the correct is the second one. I chose that but the script always prints this: `Print: Entry, ":CFBundleShortVersionString", Does Not Exist` and then a line telling me that the file `Info.plist.sqlite` (?) not found... – Duck Feb 21 '17 at 22:58
  • I see that the script is looking for the wrong file at the wrong place. I guess it should be looking at `.../Build/Products/debug/Myapp.app/Contents/Resources/database2.8.sqlite` – Duck Feb 21 '17 at 23:16