Search code examples
iosiphonefilesharing

Download created files from iPhone App's document folder on PC


I created some files which store some logging information in my iPhone App. They are stored in the App's document folder. I would like to download them onto my Mac. I'm able to see them in the xCode organizer but I'm not able to access these files. How can I achieve this?


Solution

  • First, set Application supports iTunes file sharing on on your project's settings plist. It could also be named UIFileSharingEnabled. enter image description here

    Then, for the code, save the file in NSDocumentDirectory like so:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Id its a local file in the app use its name.  If not, alter for your needs.
        NSString *fileName = @"someFile.png";
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:fileName];
     
        if (![fileManager fileExistsAtPath:documentDBFolderPath]) {
            NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
           [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
        }
        [self.window makeKeyAndVisible]; 
        return YES;
    }
    

    All taken from iOS Dev Tips - File Sharing from App to iTunes is too easy