I am currently working on a functionality to store files. I am testing the application on an iOS simulator. When I call the File Manager to create a file I get a false response. am I able to save files to the simulator?
code:
let path = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.absoluteString
let response = FileManager.default.createFile(atPath: path, contents: data, attributes: nil)
print(response)
Related
So I'm trying to rename a folder within my app, but the moveItem method is behaving strangely. Here is my code:
try FileManager.default.moveItem(at: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("parentFolder").appendingPathComponent("folderIWantToMove"), to: FileManager.default.urls(for: .applicationDirectory, in: .userDomainMask)[0].appendingPathComponent("parentFolder").appendingPathComponent("newFolderName"))
This fails and the message in the debugger is:
“folderIWantToMove” couldn’t be moved to “parentFolder” because either the former doesn’t exist, or the folder containing the latter doesn’t exist.
But when I run this in the lldb:
print FileManager.default.fileExists(atPath: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("parentFolder").appendingPathComponent("folderIWantToMove").path)
and
print FileManager.default.fileExists(atPath: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("parentFolder").path)
both return true, meaning both folders do exist. I've read some other questions with similar problems and most of them say that it is because of sandboxing. If this is the case how could I be able to change the name of and erase files within the user's document directory?
Just in case, I'm using swift 5 and running everything on an iPad with iPadOS 13 from Xcode 12 beta.
The issue there is that you are not renaming it, you are trying to move your directory from inside your documents directory to the application directory which is out of your bundle and unreachable from your app in iOS.
let document = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let parentFolder = document.appendingPathComponent("parentFolder")
let folderIWantToMove = parentFolder.appendingPathComponent("folderIWantToMove")
do {
try FileManager.default.createDirectory(at: folderIWantToMove, withIntermediateDirectories: true, attributes: nil)
print("folder and sub folder created")
print("folderIWantToMove path:", folderIWantToMove.path)
// renaming it
let newFolderName = parentFolder.appendingPathComponent("newFolderName")
try FileManager.default.moveItem(at: folderIWantToMove, to: newFolderName)
print("folder renamed to:", newFolderName.path)
} catch {
print(error)
}
I've had the same file problem, but on macOS. I fixed it by adding the "com.apple.security.files.user-selected.read-write" entitlement and setting it as true. I hope this helps! (Also, make sure to set the app sandbox entitlement to false)
I've created an iOS app that creates a "Photos" folder using FileManager, like this:
let appPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let photosPath = appPath.appendingPathComponent("Photos", isDirectory: true)
try FileManager.default.createDirectory(at: photosPath, withIntermediateDirectories: true, attributes: nil)
I know I can access that directory in the simulator by using Finder and going to:
Users/xxxx/Library/Developer/Devices/[my device GUID]/data/Containers/Data/Application/[my app GUID]/Documents/Photos
But how can I access that folder using the real device itself? (iPad or iPhone)
The native browser doesn't show my app directory
I'm trying to use realm file from the old project with the new one.
On startup I'm checking if the realm file exists in Documents directory and if it doesn't I'm just copying realm file from the bundle to the Documents directory.
This part (copying) works but if I try to open this new realm file, XCode just crashes although this file is definitely in the Documents directory (I checked).
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL
let realmURL = documentsURL.appendingPathComponent("realdata.realm")
var defConfig = Realm.Configuration()
defConfig.readOnly = true
defConfig.fileURL = realmURL
realm = try! Realm(configuration: defConfig)
This file exists, and I was able to open this with RealmBrowser from the Documents directory. File permissions wasn't changed (read/write). I'm using Realm/RealmSwift 2.10.2.
Also I've tried to create new realm, and it worked ok.
I am having problems trying to load a document into a WKWebView when the document has been added to the app using iTunes file sharing.
If I include the file inside the app I can load it fine.
I am using this code to get the load the file:
let documentsURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fooURL = documentsURL.appendingPathComponent(docFileName)
let docURL = URL(fileURLWithPath: fooURL.path)
let req = URLRequest(url:docURL)
docView!.load(req)
docURL looks like this:
file:///var/mobile/Containers/Data/Application/432E716E-F70D-4985-814C-FFE7ECE53EF8/Documents/filename.pdf
I have tried to check the file exists using this code:
FileManager().fileExists(atPath: fooURL.path)
This returns true. I have also tried to copy the file from the documents folder into the app folder but this returns an error of file not found (again this is even after checking the file exists)
Should WKWebView be able to load from this location? Or have I missed something here?
Perhaps you are looking for loadFileURL(_:allowingReadAccessTo:)
Though I didn't see it explicitly stated in the docs, it wouldn't surprise me if the security policies of WKWebView are getting in your way, and the presence of this method alone seems to confirm that ;-)
Happy coding!
I am using
let data = try?
NSString(contentsOfFile: "/Users/BenA**** 1/Desktop/textFile.txt",
encoding: String.Encoding.utf8.rawValue)
to access a file on my computer. When I test put the app on my phone, it obviously didn't work because it didn't have access to the file. How could I put this file on my phone and be able to access it from there? Thanks.
Include your "textFile.txt" in main bundle by drag and drop file in to xcode project, then access it by
class testViewController: UIViewController{
var data:String = ""
override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("textFile", ofType: "txt")
data = try? NSString(contentsOfFile: path!, encoding: ENCODING)
}}
Try this way to access 'data' in rest of code.
Hope this help.
To simulate the behavior of iOS enable the sandbox on macOS.
Now you can access files only in the container of the app.
Nevertheless the predefined folders like Documents, Library etc. still exist.
To have access to that directories there a method of NSFileManager. For example one of the most important directories in iOS is the Documents directory.
let documentsFolderURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomain: .UserDomainMask).first!
You have to use that method each time you need to get the URL of that particular directory because for security reasons the absolute path will change.