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.
Related
Question
Is it possible to determine the location a macOS app was launched from at runtime?
Context
I develop a Safari Extension, and in order for the extension to be enabled the application needs to be present /Applications/. Several users have tried to run the application directly from the DMG file and then complained that the extension doesn't appear in Safari's extension settings. I would like to detect this and alert them that they need to move the file.
You can use NSBundle, specifically the bundlePath property. The documentation says:
The full pathname of the receiver’s bundle directory.
And you can use it something like this:
NSString* bundlePath;
bundlePath = [[NSBundle mainBundle] bundlePath]
NSLog(#"%#", bundlePath);
There's also bundleURL if you want a NSURL to work with instead of a string.
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.
When I open a text document in TextEdit, quit it, rename the file and relaunch TextEdit, the renamed file will automatically be opened. The same holds true if the file is moved to a different folder.
I'm concluding from this experiment that whatever handles what documents should be automatically reopened on Mac OS X doesn't use file paths, or at least doesn't only use file paths.
So, aside from file paths, what kind of file reference I can write to a document that will remain valid even if the target file is renamed or moved while my app is not running?
I'm interested in this because I'm working on an app that might need to store references to other files inside its own documents.
You are looking for NSURL based solutions. You can create a bookmark, which will refer to the file even after it's been moved / renamed.
Borrowing from FileWatcher on GitHub by Peter Sugihara:
- (NSData *)bookmarkFromURL:(NSURL *)url {
NSData *bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationPreferFileIDResolution
includingResourceValuesForKeys:NULL
relativeToURL:NULL
error:NULL];
return bookmark;
}
- (NSURL *)urlFromBookmark:(NSData *)bookmark {
NSError *error = noErr;
NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark
options:NSURLBookmarkResolutionWithoutUI
relativeToURL:NULL
bookmarkDataIsStale:NULL
error:&error];
if (error != noErr)
NSLog(#"%#", [error description]);
return url;
}
You can safely persist the returned bookmark data, and it will point to the same file after your app has been relaunched.
Additional info in official documentation: Locating Files Using Bookmarks
EDIT: As per very valid suggestions by Peter Hosey related to error handling in the above code, a note: the above code from FileWatcher does not handle errors according to standards, and is attached to serve as a guideline.
Please refer to the official documentation for details.
I would like my OS X app to open a sample document located in the application bundle.
I'm currently doing the following:
NSString* path = [[NSBundle mainBundle] pathForResource:#"tutorial.doc" ofType:nil];
[_documentController openDocumentWithContentsOfURL:[NSURL fileURLWithPath:path] display:YES completionHandler:nil];
This works but has an unexpected effect: if I make changes to the document and save, the next time I open the bundle document the changes persist. I expected the bundle document to be read-only.
What am I doing wrong? How can I prevent this sample document to be modified?
You can use the duplicateDocumentWithContentsOfURL:copying:displayName:error: method instead. This creates a copy of the document the user can play with (and save to some other location if he wants to). This method is available only on OS X 10.7 or later though.
I've made many apps in Xcode before and always their default working directory was the one where the .app file is, so I've accessed the internal data like Whatever.app/Contents/Resources. I know it's probably not the right way, but it has always worked. Anyway, either from a recent Xcode update or for some other reason, their default working folder is now being set to "/". This only happens when I run the .app file from Finder. If I run it from within Xcode, the folder path is correct (I can set that path in the executable options, but it has no effect on what happens when you run the .app directly). Is this a setting somewhere or just the new standard?
For resources, use this cool code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"awesomepic" ofType:#"png"];
You should never depend on PWD with Cocoa. Instead, use Cocoa API's whenever possible. BSD API's should only be used if Apple provided no other way.
globheader.h
static char *appdir;
appcontroller.m
#import "globheader.h"
#implementation AppController
- (void)method {
appdir = [[[NSBundle mainBundle] bundlePath] UTF8String];
}
#end
cppcode.cpp
#include "globheader.h"
int main() {
printf("%s", appdir);
return 0;
}
toastie, why use the application bundle to store data? don't change your .app.
Instread, use the application support folder. You can modify the bytes in that folder with no problems. Read the following post from Matt Gallagher,
http://cocoawithlove.com/2010/05/finding-or-creating-application-support.html
regards,