Comments (6)
For Mac OS X and iOS you need to use the CFBundleRef and use it to access your files that are saved within the app resource bundle, because of the sandboxing issue.
Here is the code example I use to access the files from within the Mac OS X app bundle (it uses a singleton class of my own, but you can ignore that):
macbundle.hpp
#include "CoreFoundation/CFBundle.h" class MacBundle: public singleton<MacBundle> { private: CFBundleRef appBundle; bool initializeBundleLoading(); protected: MacBundle(); public: virtual ~MacBundle(); friend class singleton<MacBundle>; std::string getFullPath(const std::string& filename); };
macbundle.cpp
bool MacBundle::initializeBundleLoading() { appBundle = CFBundleGetMainBundle(); if(!appBundle) { return false; } return true; } MacBundle::MacBundle() { initializeBundleLoading(); } MacBundle::~MacBundle() { } std::string MacBundle::getFullPath(const std::string& filename) { if (!appBundle) { if (!initializeBundleLoading()) { Debug() << "Failed to initialize the bundle!"; return nullptr; } } CFURLRef resourceURL; // Look for the resource in the main bundle by name and type. resourceURL = CFBundleCopyResourceURL( appBundle, CFStringCreateWithCString( NULL, filename.c_str(), kCFStringEncodingASCII), NULL, NULL ); if(!resourceURL) { Debug() << "Failed to locate a file in the loaded bundle!"; return nullptr; } char *fileurl = new char[PATH_MAX]; if(!CFURLGetFileSystemRepresentation( resourceURL, true, (UInt8*)fileurl, PATH_MAX)) { delete[] fileurl; Debug() << "Failed to turn a bundle resource URL into a filesystem path representation!"; return nullptr; } std::string str = std::string(fileurl); delete[] fileurl; return str; }
The same code would work with no difference on iOS as well.
from corrade.
Relevant Apple documentation on Bundles:
In my code above that was written in C++, you can call the #include "CoreFoundation/CFBundle.h"
to call the C structs/functions for CFBundle* stuff. No need to use ObjC or Swift code to get the relevant information.
from corrade.
Oh, thanks a lot for the code!
Speaking about this, I have access to Apple hardware now, so I can definitely look into this and fix it. Actually, I need something like this tomorrow :D
From my experience (though using Corrade in combination with SDL2 and Magnum) the normal filesystem calls were working just fine and opening say . Seems that SDL2 is doing some magic with current working directory.data/font.ttf
properly located it in <bundle.app>/Contents/Resources/data/font.ttf
Another thing: would _NSGetExecutablePath()
do the trick too? Or is there any downside to that approach?
from corrade.
The issue is sandboxing. You have very limited places where you can download and save your own stuff, except the access of files from within the bundles.
If you try to access outside the limits imposed by the sandboxing, you get failures like user permissions needed etc.
Also look there for the C functions:
CFBundleCopyExecutableURL
http://opensource.apple.com//source/CF/CF-550/CFBundle.h
It has also additional functions allowing you to load the plugins dynamically like those CFBundleLoadExecutable and CFBundleUnloadExecutable etc.
from corrade.
Also if you see in Apple documentation the requirement to use ObjC functions, you can use associated CF functions like: NSHomeDirectory is equivalent to CFCopyHomeDirectoryURL ( http://stackoverflow.com/a/12717548 )
A solution for creating temporary files: http://stackoverflow.com/a/12714258
Also kinda incomplete C++ wrapper for CoreFoundation:
https://github.com/macmade/CFPP
Thinking more about the sandboxing, I think we are forced to use some ObjC++ to use some specific Core Foundation functions that are not available as C functions to be called from C code.
from corrade.
OSX/iOS sandbox support for Directory::configurationDir()
and Directory::home()
is in 5d58f4b. In the end it was enough to use the HOME
environment variable, so fortunately I have common code for all Unix-like platforms. Added also Directory::executableLocation()
in d6f38db which points to the location where the executable is. Apart from Linux and Windows support I used _NSGetExecutablePath()
for both non-sandboxed OSX and sandboxed OSX/iOS to avoid the need to link to CoreFoundation framework. There is now also Directory::isSandboxed()
utility function.
Opened a new issue #23 to track dynamic plugin support on iOS.
Thanks a lot for the very detailed help!
from corrade.
Related Issues (20)
- Corrade's test suite fails under AddressSanitizer HOT 9
- Corrade with BUILD_TESTS=ON compilation error: call to non-‘constexpr’ function HOT 5
- Error when installing via HunterGate HOT 7
- Prefix cmake options with CORRADE_ HOT 4
- Windows: inconsistent redefinition of _aligned_malloc HOT 3
- Corrade adds /wd*** warning disablements to "clang.exe" on windows. HOT 1
- Building Corrade with -std=c++20 causes errors inside MinGW <numbers> header HOT 3
- Corrade fails to compile with emscripten 3.1.22 HOT 4
- Opt-in to native UTF-8 support for OS interaction on Windows
- std::tuple_size / tuple_element specializations for Corrade containers HOT 1
- error: cannot initialize a variable of type 'const char *const' with an rvalue of type 'int' HOT 3
- Optimizing compilation time for the test suite -- an analysis HOT 3
- Interconnect - Slots are not called according to their record order HOT 1
- V8::Zone Allocator HOT 1
- NEON code does not build on armv7 HOT 2
- what to set CORRADE_INCLUDE_DIR to for in-source-builds HOT 2
- JsonToken::asObject() odd behavior with empty objects HOT 2
- New Release HOT 1
- Broken Interconnect on Clang-CL 16.0.5 HOT 9
- Packaging location of GDB script files HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from corrade.