8

For debugging purposes I've often written data to files on iOS using code such as this...

NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [docsPath stringByAppendingPathComponent:testName];
FILE* resultsFile = fopen([filePath UTF8String],"w");

...and then gotten the data by downloading the container via Xcode (by selecting the app on the "Window->Devices" screen, and choosing "Download container..." from the "little gear" pop-up menu just below the list of apps.)

I recall this working on iOS 9 and previous, but trying this on iOS 10 on an iPhone 6, I'm finding it doesn't work anymore. The call to fopen is returning success for /var/mobile/Containers/Data/Application/[uuid]/Documents/testname but the file isn't in the container when I download it.

Shouldn't the file be in the container? Is it elsewhere? Or is it simply not possible to dump data to a file and pull it off the phone anymore?

pkamb
  • 33,281
  • 23
  • 160
  • 191
  • What is your build target? An Extension or App? Background: I have the same issue targetting an extension: http://stackoverflow.com/questions/40463923/how-to-download-sqlite-store-file-of-imessage-extension-to-macbook – shallowThought Nov 18 '16 at 17:22
  • @shallowThought My question is for an app –  Nov 18 '16 at 17:56
  • Did not check on app in iOS 10. I assumed it is Extension specific I as my (and your) work-flow always worked in Apps. Bug or feature? – shallowThought Nov 18 '16 at 18:07
  • Same experience for me, that created folders are found in the `Download Container` archive but files are not... – pkamb Feb 12 '19 at 19:50
  • I am not using `fopen`, I have downloaded file in document directory, and downloading container is giving me the fiile – Mehul Thakkar Feb 13 '19 at 12:17

1 Answers1

0

I have tried to reproduce your problem (under iOS 10.3.3, Xcode 10.1) and it all worked on my end in the context of an App project. The issue you have may be related to what you do with the file object resultFile, if you could share some code containing the next lines of your code (or check that you are calling fclose() for example), it may be easier to solve it.

Also please note that it is seems NOT supported to write to the Docs directory from the code controlling an App Extension as detailed here: reading and writing to an iOS application document folder from extension

Code that worked in the context of an App project / target:

  • using Data in Swift like this:

    guard let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last else { return }
    
    let someData = "Hello world using swift".data(using: .utf8)
    
    do{
       try someData?.write(to: documentsPath.appendingPathComponent("hello-world.txt"))
    }catch{
        //handle the write error
    }
    
  • using NSData with Objective C:

    NSString * hello = @"Hello world using NSData";
    NSData * helloData = [hello dataUsingEncoding: NSUTF8StringEncoding];
    
    NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [docsPath stringByAppendingPathComponent:@"fileNameObjcNSData.txt"];
    [helloData writeToFile:filePath atomically:true];
    
  • using fopen:

    NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [docsPath stringByAppendingPathComponent:@"fileNameObjcFopen.txt"]; //
    FILE * fileHandle = fopen([filePath UTF8String], "w");
    
    if (fileHandle != NULL){
        fputs("Hello using fopen()", fileHandle);
        fclose(fileHandle);
    }
    

Hope it helps

Ivan S Ivanov
  • 532
  • 5
  • 10
  • I think that possibly the file is not being created if it does not previously exist. But there is no such problem for creating directories. – pkamb Feb 15 '19 at 19:06
  • Hi @pkamb, I am creating a new file with the above code in an App project, but it does not work when the above code is called from an App extension. I will edit my answer to reflect this. – Ivan S Ivanov Feb 18 '19 at 10:17