Preset NSUserDefaults - cocoa

I thought I read some where that I could include a NSUserDefaults file with a cocoa app. I cannot seem to find how to do that.
I know I could just create on first app launch but I dont want to do that just yet...

The User Defaults Programming Topics doc is your friend. You'll want to -registerDefaults:, which is going to be an NSDictionary instance created at runtime or loaded from a plist file in your app's resources folder.
As to not wanting to do it just yet, when did you plan to? If you're going to register your app's defaults at all, it's to be done at every launch.

Related

How to save file from app bundle to disk

I have a file that's in my NSBundle, and I'd like the user to be able to save it anywhere they'd like on their computer. How would I be able to do that? I'm using Xcode 8.1, Swift 3, and making a macOS Cocoa application. Thanks for your help!
Use NSSavePanel to obtain the destination URL from the user
Use NSBundle to obtain the source URL in your app bundle
Use NSFileManager to copy the file
If once you've read the documentation, or written some code, you get stuck ask a new question showing where you've got to and what your problem is.
HTH

Mac OSX: How to associate a directory as bundle

I am writing an app for Maverick.
The app creates a folder under /user/document, named "folder.db".
All the user related files will be in a folder "folder.db".
I would like to associate my app with "folder.db" directly, so that clicking on it would open my app and not the Finder.
How to achieve that?
Note: I tried to play with the UTI settings in xcode but not luck...
First .db is generally used for databases. So probably not a good idea. What you are looking for is a package or bundle in Cocoa terms. In Cocoa you want to look for the fileWrapper methods. Those create package/bundle files that are folders with a special flag bit set to make it act like an opaque file in Finder
You might want to study NSWorkSpace, NSFileManager, NSBundle, NSDocument and NSOpenPanel and NSSavePanel.
Those will get you on the path.

Resetting mac application in xcode

How can I reset a cocoa application in Xcode? When I run my application from Xcode all user default are saved. I would like to reset (delete) my application to create a new one with new settings and run as it would be run first time.
I found one way - I change a name of my app to a different one and it is started as new. But how can I do this with an old name?
You can use the defaults command line tool to remove all the settings, you just need the bundle id of your app (which you can find in your Info.plist file), e.g. if your bundle id is "com.foo.barApp" then you can run this from a terminal shell:
defaults delete com.foo.barApp
Is your app limited to the App Store? If so, I don't think there's a way to delete your entire application, specifically because of the sandbox restrictions. We'd need more information on how you are planning to distribute it to help.
If you just want to delete your app settings, then clear whatever database you store your app data in - [NSUserDefaults +resetStandardUserDefaults], SQLite, App Library directory, whatever. That is dependent on how/where you are storing user data, there's no one size fits all solution.

NSLocalizableString and info.plist

I'm creating this iOS app, in which I use the PushWoosh (www.pushwoosh.com) notification service. Notification is working fine, but now I want to differentiate notification based upon the locale of the app: I want the users of the app running their OS in English to receive a notification in English, the German users in German, etc.
For that, I register this one app several times with PushWoosh, so each localization gets its own Pushwoosh App ID. For simplicity sake, I'm aiming to have all localization stuff in one file ("localizable.strings").
PushWoosh requires to have their APPID listed in the info.plist. So what would make sense to me, is to have the value of the PushWoosh .plist key localized. This is what I did:
In the .plist, I replaced
<key>Pushwoosh_APPID</key>
<string>2B46A-F82CC</string>
with
<key>Pushwoosh_APPID</key>
<string>PUSHWOOSH_ID</string>
Then, in the localizable.strings, I added the following entry:
"PUSHWOOSH_ID" = "2B46A-F82CC";
Finally, in the code, I replaced
[[NSUserDefaults standardUserDefaults] setObject:appCode forKey:#"Pushwoosh_APPID"];
with
[[NSUserDefaults standardUserDefaults] setObject:appCode forKey:NSLocalizedString(#"Pushwoosh_APPID", nil)];
Somehow, however, when running the app, "Pushwoosh_APPID" resolves into "PUSHWOOSH_ID", rather than in "2B46A-F82CC".
All other strings in localizable.strings are called just fine, so I guess it's a syntax thing.
Concrete question: what am I doing wrong, code-wise? Also, shoot me if this is an undesirable approach in general.
Thanks in advance!
For localizing Info.plist values, you will need to create a separate strings file called InfoPlist.strings under your language-specific project directory, such as en.lproj, etc, and put the key and the translated value there, for example:
Pushwoosh_APPID = "2B46A-F82CC";
Take a look at the reference of Information Property List Key:
... you store the values for a particular localization in a strings file
with the name InfoPlist.strings. You place this file in the same
language-specific project directory that you use to store other
resources for the same localization. The contents of the
InfoPlist.strings file are the individual keys you want localized and
the appropriately translated value. The routines that look up key
values in the Info.plist file take the user’s language preferences
into account and return the localized version of the key (from the
appropriate InfoPlist.strings file) when one exists. If a localized
version of a key does not exist, the routines return the value stored
in the Info.plist file.
FYI Pushwoosh provides the Multi-language support for sending notifications in the language, which is set in the OS. Its based on Tags, and can be used both via the Control Panel and API. I suppose it would be way easier not to reinvent the wheel :)

MacOS X: Remove plist file when the app is removed

I am showing a license form at app startup, but it should be shown only at the first time the app launched. Not always. So, i tried to store a flag in plist or user defaults, but upon the app removing(move to trash), plist or userdefaults is not getting removed from system, thus i'm not able to achieve my task.
This is what i have been trying below,
I am storing a flag in plist file and save the file(in Documents directory) during App launch, so next time , i can read from there and find out whether the app is running first time or not. I am failing here, because even the app is removed(move to trash) from system, i am not able to remove this plist file.
I tried to set a flag in NSUserDefaults too, but after deleting the app and build again and launch the app, it still persists the previous value i stored. I am wondering, NSUserDefaults doesn't get deleted when app is deleted from system?
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:#"1" forKey:#"APPLAUNCHED"]; // store user default
[ud synchronize];
Could someone please advise?
Thank you.
Neither user defaults nor the application sandbox get automatically removed when the application is deleted. I think there's no way to do that.

Resources