views:

252

answers:

6

Is anyone aware of any sample or open-source code that does this? Or a write-up explaining how to do it?

Apple has quite a bit of sample code that opens web addresses in other apps. But I can't find any examples where the app asks another app to open a file that is stored on the phone.

Thanks.

A: 

Apps cant access each other's files.

sylvanaar
+2  A: 

The iPhone SDK has no officially-sanctioned shared repository — every app is sandboxed. If data can't be passed with a URL scheme, then it must be done through "the cloud". Stanza got around this by storing some data in the DCIM folder (which holds the user's photos), but Apple forced them to remove the functionality just a few days ago.

There are rumors that we will get a new data store in the 4.0 SDK, but nothing solid.

Brock Batsell
I see a fileURLWithPath: isDirectory: method in the NSURL class reference. Does this help?
William Jockusch
No. You will only be able to access files within your app's bundle (what "sandbox" means), which, likewise, other apps won't be able to read.
Brock Batsell
+1  A: 

My understanding (although I could totally be wrong about this) is that apps can access the files of other apps, but that finding them is the problem, since each app's directory is assigned a new UUID as it's installed, and apps can't list the contents of the /Applications directory.

If you're writing both apps yourself you may be able to pass the location of one app's file to the other app using a custom URL scheme. Also if the file is sufficiently small, you could pass it as part of the URL.

Frank Schmitt
Although I have not tested this, I'm pretty sure this is correct - I remember at WWDC Apple talking about efficient passing of large data to other applications when using the URL scheme, and I know files were involved somehow - perhaps to the TMP directory instead?
Kendall Helmstetter Gelner
+5  A: 

There's two ways to do it.

1) If you can encode your file as a string, you can pass it to another app via an NSURL. The receiving app, "ReceivingApp" must implement the

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 

in it's application delegate. Send your data using the

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"receivingapp://your.data.goes.here"]]];

2) Use the pasteboard.

I would use the custom URL scheme since it 1) will save you a few steps and 2) it doesn't give the rest of the OS access to your data should something go amiss with the receiving app. I'm not sure what the character limit on an NSURL is, but it's pretty big.

kubi
I think the pasteboard may have been what I heard Apple talking about at WWDC, in terms of large amounts of data actually being stored in a file somewhere for efficency. I think you'd still use the URL scheme, just with some kind of flag to let the other app know to check the pasteboard along with a key of what to look for.
Kendall Helmstetter Gelner
Exactly. That's what I meant by "URL scheme... will save you a few steps" Even if you use the pasteboard, you'll still have to use the URL Scheme to let the other app know that it's time to read from the pasteboard.
kubi
+1. Don't know how I didn't know about UIPasteboard. That's what I get for not writing for the iPhone for awhile. Obviously, using a URL scheme if at all possible would still be the best option.
Brock Batsell
A: 

To expand a little on a response to Frank, I think you can use a custom URL with a file path - but I think the path you should use would be some place in the Caches directory:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];

I am presuming other applications could see into this same space.

Kendall Helmstetter Gelner
A: 

Besides the custom URL scheme that the other answers mentioned, you could hack your way into this by storing the data encrypted within the fields of a temporary contact that you can create through the AddressBook API.

luvieere