Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-07-2011, 12:23 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 2
milani is on a distinguished road
Default Automatically store files in Documents directory for iTunes file sharing?

Hello everyone!

I have been search for the answer to this question for quite some time without any luck.

Here is what I'm trying to accomplish: I would like to - as the developer - store files by default inside of the Documents directory of my app so that when a user downloads my app those files will show up in iTunes file sharing.

I have tried a variety of things, but my lack of familiarity with Xcode and iPhone programming isn't helping me.

Essentially what I want to do is store a zip file inside of my app, and then have that zip file appended (or just added, I guess) to the Documents directory - either automatically or by some sort of basic user interaction. Is this possible?

If someone can help me out with this I would really really appreciate it! Thank you!
milani is offline   Reply With Quote
Old 08-07-2011, 07:41 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 70
andrewtheeditor is on a distinguished road
Default

Welcome to the forum! This is much easier than you'd think. You just need to find the path to the file from within the bundle, path to the documents directory, and then copy the file. You can even check to see if the file exists there already. I had a PDF I wanted to place in the directory if no other documents existed. This way, the user could delete my file and put their own there. As long as something existed there, I wouldn't add my file. Here is how I did it:

Code:
// Get documents Directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *publicDocumentsDir = [paths objectAtIndex:0];

// Check the File Count
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:publicDocumentsDir error:NULL];
if (files.count == 0) {
    // Directory is empty -- add my file
    // Find the file in the bundle
    NSString *originalFile = [[NSBundle mainBundle] pathForResource:@"DefaultPDF" ofType:@"pdf"];
    // Get the path I want to save it to
    NSString *newPath = [publicDocumentsDir stringByAppendingPathComponent:@"DefaultPDF.pdf"];
    // Save it
    [[NSFileManager defaultManager] copyItemAtPath:originalFile toPath:newPath error:nil];
}

Last edited by andrewtheeditor; 08-07-2011 at 07:43 PM. Reason: fixed the files.count check. I actually checked for <= 1 in my app because another directory existed there.
andrewtheeditor is offline   Reply With Quote
Old 08-12-2011, 09:29 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 2
milani is on a distinguished road
Default

Thanks so much for your quick reply! I will take a look at the code and see what I can do. I may be back if I can't figure it out but I'll do my best. Thanks again!

Edit: just to clarify my situation, I'm using PhoneGap to develop the app (I'm not confident enough developing with Xcode natively). I'm not entirely sure where to place this code. Would I place this in the AppDelegate.h and/or AppDelegate.m?

For what it's worth I tried placing the code snippet in both .h and then .m, and in both cases I received several error messages. I'm sure the code is fine, but I'm probably doing something wrong. Any other help you could provide would be awesome. (Like I said, I'm as green with Xcode as you can get - and I have no prior knowledge of any languages beyond PHP).

Edit 2: by the way, since this seems relatively easy to do (if you know what you're doing) I'd certainly be willing to pay someone via PayPal for the hour or two that it takes to setup (sorry if this breaches TOS of the forums, but I'm to the point that I just want to get it done so I can launch the app)!

Last edited by milani; 08-13-2011 at 11:13 PM.
milani is offline   Reply With Quote
Reply

Bookmarks

Tags
documents, file, itunes, sharing, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 396
14 members and 382 guests
7twenty7, blasterbr, buggen, chiataytuday, dedeys78, dre, fiftysixty, HemiMG, jimmyon122, jonathandeknudt, LEARN2MAKE, pungs, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:04 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0