How to find files and folders in MAC volume using App sandboxed application - xcode

I am creating a sand-boxed application on MAC(10.9).When App Sand-boxed is off then i am able to get files and folders from any path. But when I does App Sand-boxed enabled it's not accessing any files and folders from any path.
Getting file and folder from path ("/Volumes/DriveName" or "/" etc.) when App Sand-boxed is off using Qt 5.0
// for folders
QStringList folderlist;
QDir curDir = QDir(path);
curDir.setFilter(QDir::Dirs);
QFileInfoList list = curDir.entryInfoList();
for(;<list.size;)
{
QfileInfo fileInfo = list.at(i);
folderList << fileInfo.filePath();
}
// for files
QStringList filelist;
QDirIterator dirIn(path,filterfiles,QDir::AllEnteries);
while(dirIn.hasnext())
{
dirIn.next();
QfileInfo fileInfo = dirIn.fileInfo();
filelist<< fileInfo.filePath();
}
What i do same when App sand-boxed is enabled?

That's the point of a sandbox, you can only access resources within the sandbox. A user may add files to the sandbox, but that requires your application to launch a file open dialog box, so they can choose the file they want to work with.
I suggest you start by reading the Apple documentation here.

Related

Cannot Create New Directory in MacOS app

I am working on a mac app. I have been struggling to create a directory in "Desktop" on Mac OS. I am working on High Sierra. It was working fine, but suddenly the app is unable to create a directory in Desktop. I get the following permission error. I am using Xcode 9.3 and swift 4.1.
Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “TestDir” in the folder “Desktop”." UserInfo={NSFilePath=/Users/sj/Library/Containers/com.user.TestSync/Data/Desktop/TestDir, NSUnderlyingError=0x60400044cc60 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"
Here is my code:
class DBManager: NSObject {
static func getDirectoryPath() -> URL {
let homeDirectory = URL(fileURLWithPath: NSHomeDirectory())
let desktopPath = homeDirectory.appendingPathComponent("Desktop")
let databaseFolder = desktopPath.appendingPathComponent("TestDir")
return databaseFolder
}
static func createFolderIfNotExist() {
let databaseFolder = DBManager.getDirectoryPath()
if !FileManager().fileExists(atPath: databaseFolder.path) {
do {
try FileManager.default.createDirectory(atPath: databaseFolder.path, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
}
}
}
The code works fine if I am to create a directory inside "Documents". But for some reason, I am not able to create inside Desktop even if I have provided read/write access for Desktop to everyone. I have also included the screenshot of the directory where I have to create one.
I tried to create a directory inside "Downloads" but ended with same permission issue. But, again I was successful to create directory inside "Library". So, it seems that the OS is not giving permission to create inside alias directory since both Desktop and Downloads are alias.
Any help is highly appreciated.
That is because your macOS Application is running in the "App Sandbox", check your "Capabilities" tab in the target settings.
~/Library/Containers/%BundleID%/Data/Desktop is a soft link to the user's desktop, and you have no write permissions (depending on your entitlements file not even read permissions).
Depending on your use case you can
disable the sandbox mode
let the user pick a folder by opening an "Open" dialog (then you can write to this)
enable read/write in some other protected user folder (like Downloads, etc.) or
create the TestDir directly in your home directory without using any soft linked folder
Besides what mentioned by #ChaosCoder, we also need set NSOpenPanel.canCreateDirectories to true:
panel.canCreateDirectories = true
Delete the App Sandbox data from your Capabilities.
Click on your project in your file navigator, then your project in the TARGETS tab, then make sure "Signing & Capabilities" is selected, then "ALL" then double click the "x" next to "App Sandbox".

Code a File Path that works in both OSX and Windows (dotnet core)

I have to save files in my dotnet core application. I originally developed in windows; but when I ported the code to try developing on a mac, the file path is no longer valid (i.e. "c:\content..."). Is there a way to reference a file path, in my code, that will work in both situations? We deploy to azure... so the windows file path must there too.
FileInfo fileInfo = new FileInfo("c:\\Content\\SalesOrderOutput\\" +
fileName.txt);
FileStream stream = fileInfo.OpenWrite();
Use Path.Combine method like this:
internal static readonly FileInfo Mp4WithAudio = new FileInfo(Path.Combine(Environment.CurrentDirectory, "Resources", "input.mp4"));
Code from https://github.com/tomaszzmuda/Xabe.FFmpeg/blob/c8cc4232b5afa2860ede3be63a680d754ed73002/Xabe.FFmpeg.Test/Resources.cs

Cannot find content file in bin folder

I have a IIS Express project running in debug mode in Visual Studio 2015.
The web application references a Class Library which has this line of code:
var certificate = new X509Certificate2(#"certkey.p12", "notasecret", X509KeyStorageFlags.Exportable);
The certkey.p12 file is in the same folder as the source code for the class library as is marked as "Build Action = Content" and "Copy if newer".
When I build, the file is copied to the web app's bin folder as expected. But when I call the method from the web app, it throws "file not found".
It works fine in Unit Testing where the file is copied to /bin/x64/Debug/
So where is IIS Express looking for the file? I suspect it is some sort of temp folder but why isn't VS copying the file there when I build/debug?
When running under IIS Express, it was looking for is "C:\Programs Files\IIS Express\certkey.p12".
I used a little peeking to find the folder that the file was actually copied to and came up with this:
string binPath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
Uri uri = new Uri(binPath);
string localPath = uri.LocalPath;
string dir = System.IO.Path.GetDirectoryName(localPath);
// now "dir" is the full local path of the bin folder
It is a little slow on first execute (~100ms) but faster on subsequent requests.
If there are any problems when I deploy this to production, I'll update.

Packaging mp3 files into Qt app

Since I found mp3 files can't be retrieved by QMediaPlayer from resource, I figure it out to use them as local file. I use this cmd with a little modification to copy files into installation directory:
copydata.commands = $(COPY_DIR) $$PWD/resources/sound $$OUT_PWD/HomeControl.app/Contents/MacOS
first.depends = $(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata
So those files existed in the /HomeControl.app/Contents/MacOS/sound. From Qt Creator the app plays the mp3 perfectly, but if executed from the build directory it doesn't play the mp3 at all! Really don't know why.
arm away btn clicked
current media: "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Arm_Away.mp3"
disarm stay btn clicked
current media: "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Disarmed.mp3"
Is there any difference from executing in the Qt creator & directly from the App? Like debug mode or what?
Or is there other ways to correctly deploy mp3 files into the bundle?
This is how I build in resources for OS X bundles:
mac {
Resources.files = dirInTheProjectDirectory
Resources.path = Contents/MacOS
QMAKE_BUNDLE_DATA += Resources
}
You can call Resources whatever you fancy.
That copies them to Contents/MacOS/dirInTheProjectDirectory. You can then access them with QDir(QCoreApplication::applicationDirPath()+"/dirInTheProjectDirectory/");
The likely problem is when you're accessing them in your code you're pointing to the path in the build directory, and not the path that's within your bundle.
Here's my final solution. Thank you #nicholas-smith.
In the .pro file:
# deploy with mp3 folder
mac {
Resources.files = ./resources/sound
Resources.path = Contents/MacOS
QMAKE_BUNDLE_DATA += Resources
}
Then accessing those file with:
// pass the path of mp3 to the QMediaPlayer
playAudio(QCoreApplication::applicationDirPath()
+ "/sound/System_Disarmed.mp3");
From log it shows:
arm away btn clicked
current media: "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Arm_Away.mp3"

Where is the "App Bundle" looking for my media folder? Folder found when running app from Qt Creator, "file not found" when using Terminal

I'm having a little trouble making my app bundle work from the terminal or just double clicking it.
This App actually compiles, links and runs perfectly within the Qt Creator IDE. But, if I try to open it from the terminal I get a "media/file.x file not found" error. The App bundle nor the /Contents/MacOS/executable is finding the "media" folder that is supposed to be beside the executable.
In my app I do something like:
openFile("media/file.x");
In Windows and Linux, this file WILL be found if the "media" folder is exactly in the same hierarchical position of the executable (beside it). On the Mac I have discovered it works differently cause Qt Creator builds an "App Bundle" and the actual executable is inside the /Contents/MacOS folder, so I copied the "media" manually there. This worked without any hassle when "playing" my app from the Qt Creator but as mentioned before it doesn't work when running the bundle itself.
So does anyone know where or how can I homogenize the look for this "media" folder so it works on both: Qt Creator and the App bundle?
Lately, I have been using the following command to "install" the folder on the bundle.
mac {
MediaFiles.files = media
MediaFiles.path = Contents/MacOS
QMAKE_BUNDLE_DATA += MediaFiles
}
Thanks for your help.
After looking for a few days I found a couple of posts discussing the relative path problem. I was just not searching with the right words... The answer is displayed in:
Relative Paths Not Working in Xcode C++
http://www.experimentgarden.com/2009/06/how-to-load-resource-from-your.html
Basically it is necessary to add this:
//On the include part
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif
// On the main of your app
// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
#ifdef __APPLE__
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
char path[PATH_MAX];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
{
// error!
}
CFRelease(resourcesURL);
chdir(path);
std::cout << "Current Path: " << path << std::endl;
#endif
// ----------------------------------------------------------------------------
IMPORTANT:
The chdir, changes the working path of the app, so it is not necessary to change you relative code after that...

Resources