views:

240

answers:

5

What code could I use in an iPhone app to get and set the settings of another app I wrote? (preferably using NSUserDefaults)

+3  A: 

You simply cannot do that. Each application is installed into its own folder and is given its own, unique user id. The file containing these settings is in the other application's folder and its permissions are set to that of the other application. The only way to access the data is to use the same application identifier as the other application, in which case installing your application would overwrite the old application.

EDIT:
This solution was given when the question was asking to do this using NSUserDefaults, specifically. For the updated question, the keychain approach or the server approach provided are both reasonable.

Michael Aaron Safyan
+4  A: 

You're not going to be able to pull this off with NSUserDefaults.

The Keychain, while somewhat cumbersome in its C-ness and much more limited than the NSUserDefaults API, might allow you to accomplish this. If you can serialize whatever you need to share between your apps into a few strings, it might be worth trying.

From iPhone OS 3.x Release Notes:

It is now possible for you to share Keychain items among multiple applications you create. Sharing items makes it easier for applications in the same suite to interoperate more smoothly. For example, you could use this feature to share user passwords or other elements that might otherwise require you to prompt the user from each application separately.

Sharing Keychain items involves setting up the proper entitlements in your application binaries. Using Xcode, you must create an Entitlements property list file that includes the supported entitlements for your application. The process for creating this file is described in iPhone Development Guide. For information about the entitlements you can configure, see the description for the SecItemAdd function in Keychain Services Reference.

Accessing shared items at runtime involves using the Keychain Services programming interface with the access groups you set up during development. For information about how to access the Keychain, see Keychain Services Programming Guide.

Here's Buzz Anderson's Simple iPhone Keychain Code. You could use it to store key/value pairs as strings in the keychain. It's not much, but perhaps better than nothing. See Apple's Keychain Programming Guide for more.

Danilo Campos
A: 

You can have one app send the data to your server, then the other app can get the data from your server.

You can't do this using NSUserDefaults but it can be done.

Jay
A: 

You could use a shared clipboard. It wouldn't be secure, but both apps could read and write from the same clipboard. You just need to create an application specific UIPasteboard. Check out the UIPasteboard class reference on Apple's developer site for more info.

--Mike

Hivebrain
A: 

You should definitely have a look at UIPasteboard, as suggested – you can create a new pasteboard for use by the applications you are creating (though nothing will stop other apps using them, but people are faily unlikely to). A UIPasteboard is persistent through a power cycle / reboot – it will exist until the creating application is deleted.

You could also have a look at the SwapKit libary (which looks very cool):

http://infinite-labs.net/swapkit/

Benjohn Barnes
To be more precise: you can post a "manifest" (app registration) via SwapKit that all other SwapKit apps can see. This is usually a static dictionary containing stuff like type-to-app bindings, but you can add your custom keys so long that they don't start with "ILApp". (Use [ILSwapService registerWithAttributes:(dict) update:YES] to change programmatically). ~ the SwapKit author.
millenomi
… (and [ILSwapService registrationForApplicationWithIdentifier:] to read. API docs: http://infinite-labs.net/swapkit/docs/api/interface_i_l_swap_service.html)
millenomi