Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Swift playground on iPadOS seems not support file operations

I am developing an app on the iPadOS’s Swift playground. And I found that by using FileManager, I can not create/edit any file even within bundle.

my code:

func createNewFile(){


    let filePath = Bundle.main.bundlePath


    let fm = FileManager()


    var success = fm.createFile(atPath: filePath+"/de.txt", contents: "tt".data(using: String.Encoding.utf8), attributes: nil)


    print(success)    


}

and the result is always false.


is it because the Swift playground on iPadOS currently do not support file operations? It can read a file from resources, but can not write.


According to what I know, it should be possible to make Swift playground on iPadOS to do file operation, because the Python interpreter on iPadOS (such as Pyto), and also C++ complier on iPadOS, etc, all can do file operations within in its app directory. I tried, no problem. It can read, write, delete files.


Best regards

Iven Dong

iPad Air, iPadOS 16

Posted on Aug 7, 2023 7:46 AM

Reply
Question marked as Best reply

Posted on Aug 7, 2023 12:43 PM

You cannot save files in the app bundle. The app bundle is read-only.


You must choose a different location to save files in your app. The Application Support and Documents directories are common places to save files in iOS apps. The FileManager class has a url function that returns the URL of a directory you specify. The following code locates the user's Application Support directory:


let fileManager = FileManager.default
        
do {
  let userApplicationSupportFolder = try fileManager.url(
    for: .applicationSupportDirectory, 
    in: .userDomainMask, appropriateFor: nil, create: true)
  // Add your code to save the file here.
} catch {
  print("Failed")
}


If you want to save someplace else, replace .applicationSupportDirectory with the folder where you want to save the files.

Similar questions

2 replies
Question marked as Best reply

Aug 7, 2023 12:43 PM in response to IvenDong

You cannot save files in the app bundle. The app bundle is read-only.


You must choose a different location to save files in your app. The Application Support and Documents directories are common places to save files in iOS apps. The FileManager class has a url function that returns the URL of a directory you specify. The following code locates the user's Application Support directory:


let fileManager = FileManager.default
        
do {
  let userApplicationSupportFolder = try fileManager.url(
    for: .applicationSupportDirectory, 
    in: .userDomainMask, appropriateFor: nil, create: true)
  // Add your code to save the file here.
} catch {
  print("Failed")
}


If you want to save someplace else, replace .applicationSupportDirectory with the folder where you want to save the files.

Aug 8, 2023 6:15 AM in response to Mark Szymczyk

Hi Mark


Thanks for telling me this. You are quite helpful.


I did a try and find that all the directories are not available for iPadOS, maybe due to the sandbox techniques of iPadOS. Only these directories are available to read and write: application support, document, documentation, cache, etc.


Well, now I can do what I want. Thank you very much.


Best regards

Iven Dong

Swift playground on iPadOS seems not support file operations

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.