Failed in deleting file in main bundle under sandbox - macos

I am trying to delete a file in the main bundle of my app in code.
What I am trying is: get the file path via [[NSBundle mainBundle] pathForResource:ofType:], then try to delete it via [[NSFileManager defaultManager] removeItemAtPath:].
My problem is, my app is sandboxed, so I got information like this: sandboxd deny file-write-unlink ...
Is there a way to overcome this? Any idea would be appreciated.

You cannot delete files in your own sandbox as for one thing it will break the digital signature used to verify the validity of your app.
If you want to have files that can be manipulated in this way then you need to copy them from your bundle into, say, the caches directory, where you can do whatever you like to them.

Related

Where is my database located of my MacOS app?

What is the database location of a MacOS application when using Core Data ?
I searched everywhere on my Mac and did't find it.
I Have the hidden files OFF and I'm sure there is data in my database.
Also I don't use app Sandbox.
If you have sandboxing enabled for your app, it will be placed under ~/Library/Containers/app.bundle.id/Data/Library/Application Support/AppTargetName where app.bundle.id is the Bundle Identifier specified in your app's target and AppTargetName is the name of that target, i.e. the name of the generated .app file. In that folder you should find the SQLite files that contain the database data.
Look for the persistentStoreCoordinator method in your AppDelegate.m. There is a line
NSURL *applicationDocumentsDirectory = [self applicationDocumentsDirectory];
Just add
NSLog(#"myDirectory: %#", applicationDocumentsDirectory);
This assumes you started your project with Xcode 8 Cocoa template with "use Core Data" option.
Or add
NSLog(#"Array of CD stores: %#", self.persistentStoreCoordinator.persistentStores);
to applicationDidFinishLaunching, for example. The resulting path should be in your user's library Users/<user>/Library/Application Support/<whatever>/<appname>.storedata.
Ask your NSPersistentStoreCoordinator.

URLForResource returns nil

I'm in the process of learning how to use xcode and how to write applications for the Mac.
So, I have a simple screensaver that loads an external file which works fine. I decided that it would be better to embed the html source in the application to reduce the external dependancies.
I dragged the HTML file to the resources folder, it's at the top level there's no sub folder or anything.
I made sure the Add to targets had my application in it, along with selecting 'create folder references' and 'copy files if needed'. I use the following to get the url to the file, but it returns nil
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:#"embedded" withExtension:#"html"];
System Preferences[10185]: fileUrl returned (null)
I've read through all the other SO issues for the similar problems but none of the fixes seem to apply. I've gone through and checked them.
If I show the package contents of the final build the embedded.html is in there. Am I missing something really obvious?
edit to add
In build phases it's listed in Copy Bundle Resources
xcode version is 6.1.1
After some considerable digging and a smidgeon of luck I found that
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:#"embedded" withExtension:#"html"];
is incorrect and I should be using
NSURL *fileUrl = [[NSBundle bundleForClass:[self class]] URLForResource:#"embedded" withExtension:#"html"];
instead.
i.e. NSBundle bundleForClass:[self class] and not NSBundle mainBundle will give access to a screensaver bundle.
Might be obvious, but only if you know ;)
See Apple Developer NSBundle Class Reference
I had a similar problem. The answer for me was to check the Target Membership of object's identity.
Quitting and restarting Xcode fixed this for me.
using this solved my problem [myBundle URLForResource: withExtension: subdirectory: ]
Subdirectory queries to NSBundle URLForResource need to specify the subdirectory. Otherwise it returns nil.
One hour figuring this today :'(
I met a same issue in a MAC application. After several hours, I found an easy way to fix it.
1, Make sure there is a phase named "Copy Files" under "Build Phases". If not, please add one.
2, Add your resources into the phase.
3, Rebuild your project and you can find your resources located in ~/Library/Developer/Xcode/DerivedData//Build/Products/Debug/.

Quickly find the full path to an app whose name contains a specific phrase

I'm working on a little app that acts as a plugin for a 3rd party app. For it to function, I need to access a saved file from the 3rd party app ("other app" from here). In newer versions, the other app keeps this in ~/Application Support/Test App/Saved Files/.
This is easy to go to, using something like this:
NSString *savedFiles = [NSHomeDirectory() stringByAppendingPathComponent:#"~/Application Support/Test App/Saved Files"];
But I also want to make it compatible with older versions, and they don't use the application support folder. Older versions went in their folder in the user's applications folder, for example this structure:
~/Applications/Test App/Test App.app
~/Applications/Test App/Saved Files/
I can use the same as above to find the location to saved files. But the app won't always be installed here, or the folder name could be different in a different language. This is the problem.
I thought so far, I thought the following are options to get the path:
Ask the user to navigate to the file with a file selector from
within my app.
Ask the user where the "Test App.app" is, and get the
path via "../Saved Files/" relative to that.
Can the app get the path to a previously run application via the system?
Can the app use spotlight to quickly find the location?
Are there other ways to do this automatically, reliably, and quickly?
1 and 2 are not preferred, as that would require the user to "work" to give the app the path. What do you all think?
Are there other ways to automate this?
Which would be the fastest?
You can try to ask LaunchServices for the app's location, given its bundle identifier with the LSFindApplicationForInfo call:
CFURLRef urlRef = NULL;
LSFindApplicationForInfo(kLSUnknownCreator, (CFStringRef)#"com.example.some-app", NULL, NULL, &urlRef);
NSLog(#"URL: %#", (__bridge id)urlRef);
This assumes that the bundle identifier is the same between the two variants you mention. If it's really just the name that is the same, you could query by name instead:
CFURLRef urlRef = NULL;
LSFindApplicationForInfo(kLSUnknownCreator, NULL, (CFStringRef)#"Test App.app", NULL, &urlRef);
NSLog(#"URL: %#", (__bridge id)urlRef);

SDL with Xcode requires absolute paths

I'm trying to make an SDL application in Xcode, but I'm having trouble loading images. I'm using this template, because I couldn't get it to work when made from scratch.
Whenever I try to load an image with SDL_LoadBMP however, it returns NULL unless I give the absolute path. (/Users/Cole/code...) I looked in the exported .app file, and it does have the image I want to load in Contents/Resources/, and I've tried every combination I can think of to get at those (../Resources/image.bmp, ect.) but I can't seem to get it working.
Does anyone have a solution? I'm running Mac OS 10.7 with Xcode 4, so I can't use the templates that is within the SDL download.
Also, I tried using SDL_ttf, but I get this error:
warning: Unable to read symbols for #executable_path/../Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf (file not found).
warning: Unable to read symbols from "SDL_ttf" (not yet mapped into memory).
There does not happen to be a Frameworks folder where it's looking, but somehow it finds the regular SDL framework just fine.
You can get the path to your the Resources directory containing your file with
NSString *path = [[NSBundle mainBundle] resourcePath];
or alternatively (in theory more clean as it can access localized files) you can get the full file name with
NSString *file = [[NSBundle mainBundle] pathForResource:#"image.bmp" ofType:nil];
You'll need to pass the C string to SDL_LoadBMP, so either of the two:
SDL_LoadBMP([[path stringByAppendingString: #"/image.bmp"] UTF8String]);
SDL_LoadBMP([file UTF8String]);
I had the same problem and found a way without using any objective-c.
In xcode click on your target then go onto the build phase section
Then in the top bar click: Editor -> Add Build Phase -> Add Copy Files Build Phase
Now change the destination of the newly created phase to "Products Directory" and then add any subpaths if needed.
All you need to do now is add your image onto the list below and it should work!

Mac App Store - can't save a file

Are there any restrictions as far as saving files when you distribute an app over the Mac App Store?
If I compile and run my app from my computer it works fine - it saves the configuration.
However the version that was downloaded over the Mac App Store is not saving the configuration. I don't even see the config file. Anyone knows what is going on?
This is the code that saves the config:
-(void)saveConfig:(id)plist {
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingString: CONFIG_FILE_NAME];
NSData *xmlData;
NSString *error;
xmlData = [NSPropertyListSerialization dataFromPropertyList: plist
format: NSPropertyListXMLFormat_v1_0
errorDescription: &error];
if(xmlData)
{
if (![xmlData writeToFile:path atomically:YES])
NSLog(#"Failed to write the config file onto the hard drive");
}
else
{
NSLog(error);
}
}
You cannot write files to the application bundle directory if you’re targeting the Mac App Store. The bundle is supposed to be immutable.
Consider saving your configuration with NSUserDefaults or, if you truly need a separate file, the officially recommended location is (~)/Library/Application Support. Matt Gallagher wrote a nice post called Finding or creating the application support directory in which he provides a solution that uses standard NSApplicationSupportDirectory followed by the executable name.
Generally, you should assume that your application's assets are read-only. This is true in general, not just for the app store.
If you want to save user settings as a property list, use NSUserDefaults instead of modifying files inside the application. This will "just do the right thing", which is to save preferences to ~/Library/Preferences.

Resources