I just drag the Data.csv file to the application folder in the Navigator panel, I am trying to set the correct path of the file into the app.
The code below I used for the simulator and works perfect, but to run in the device I changed to the second block of code, then I got this errors:
Data[399:157757] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme
Error Domain=NSCocoaErrorDomain Code=256 "The file “Documents” couldn’t be opened."
UserInfo={NSURL=/var/mobile/Containers/Data/Application/C7756542-6922-4C6F-A98E-C6F407B2063E/Documents}
//code to show the path in the simulator:
guard let remoteURL = NSURL(string: "/Users/mbp/Library/Developer/CoreSimulator/Devices/7F25FC7C-F2B2-464E-85B4-A2B96DB83F17/data/Containers/Bundle/Application/F285940D-7776-4EE2-83A1-D54DD3411E0E/Data.app/Data.csv") else {
return
}
Block to run the app in the device:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let sourcePath = NSBundle.mainBundle().pathForResource(“Data”, ofType: "csv")
print(sourcePath)
let filename = "Data.csv"
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let destinationPath = documentsPath + "/" + filename
do {
try NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath)
} catch _ {
}
Try to load the file
let fetchRequest: NSFetchRequest = NSFetchRequest(entityName: "DataEntity")
fetchRequest.fetchLimit = 1
do {
let result = try managedObjectContext.executeFetchRequest(fetchRequest)
if result.count == 0 {
preloadData()
}
} catch let error as NSError {
print("Error: \(error.domain)")
}
func preloadData () {
guard let remoteURL = NSURL(string:NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]) else {
return
}
}