Swift: copying files gives Cocoa Error 262 - ios

I'm trying to move my files up a level in some random directory in the default Documents directory. But when copying, Swift gave me the error Cocoa Error 262: NSFileReadUnsupportedSchemeError. Why did I get this error? I ran a quick search and pretty much everyone who'd encountered this problem was trying to copy files out of the camera roll, but I'm not. Any thoughts? Thanks in advance.
Code here:
private func copyFilesInDirectory(fromDir: String, toDirectory toDir: String, withCompletionHandler handler: ()->()){
println("from: \(fromDir)\nto: \(toDir)")
var error: NSError?
var contents = fileManager.contentsOfDirectoryAtPath(fromDir, error: &error)
if let dirContents = contents{
let enumerator = (dirContents as NSArray).objectEnumerator()
while let file = enumerator.nextObject() as? String{
let filePath = fromDir.stringByAppendingString("/\(file)")
println("copying \(filePath)")
if(fileManager.copyItemAtURL(NSURL(fileURLWithPath: filePath)!, toURL: NSURL(string: toDir)!, error: &error)){
println("COPIED")
}
else{
println("COPY ERROR: \(error!.localizedDescription)")
}
}
handler()
}
}
and here's the log if anyone's interested:
from: /Users/dolce/Library/Developer/CoreSimulator/Devices/05566649-BABB-44BE-BE6F-AFF7B41E3065/data/Containers/Data/Application/F5A83C94-C177-4826-BDD5-3A50E7508239/Documents/Hello/tmp
to: /Users/dolce/Library/Developer/CoreSimulator/Devices/05566649-BABB-44BE-BE6F-AFF7B41E3065/data/Containers/Data/Application/F5A83C94-C177-4826-BDD5-3A50E7508239/Documents/Hello
copying /Users/dolce/Library/Developer/CoreSimulator/Devices/05566649-BABB-44BE-BE6F-AFF7B41E3065/data/Containers/Data/Application/F5A83C94-C177-4826-BDD5-3A50E7508239/Documents/Hello/tmp/test.png
COPY ERROR: The operation couldn’t be completed. (Cocoa error 262.)

The toURL parameter of copyItemAtURL() must not be the destination
directory, but the URL of the destination file. This can be
created with
let destPath = toDir.stringByAppendingPathComponent(file)
You should also replace
let filePath = fromDir.stringByAppendingString("/\(file)")
with
let filePath = fromDir.stringByAppendingPathComponent(file)
There is also a copyItemAtPath() method which saves you from
converting the paths to URLs.

On an iOS device, the simple answer is you can't do that. On the simulator things are little different. What you're probably dealing with is file permissions on the directory you're trying to write to.
As a general rule on iOS, you can write to the designated directories like the documents/caches and nothing else.

Related

Access Windows/Mac Shared Folder Locally With smb from iOS

I am trying to build an app where I am able to access(read/write) windows/mac shared folders in my local network with swift.
Is there any possible way to do that with swift?
There is an App in the App Store called "FileExplorer" https://apps.apple.com/de/app/fe-file-explorer-file-manager/id510282524 where you can access these shared folders, but I do not know how they programmed this and with which language.
I also tried to access my shared folders via this App and yes it worked I can see my shared folders on my Phone.
But there needs to be a way to do it with swift...
I already tried different things(code bellow).
In the code bellow I tried to access the shared folder of my second mac and write the Text "Write this text to the fileURL as text in iOS using Swift" into the file named "Test.txt" and after that I want to read the same file again.
#IBAction func Button(_ sender: UIButton)
{
var uc = URLComponents()
uc.scheme = "smb"
uc.user = "user"
uc.password = "password"
uc.host = "ip-adress"
uc.path = "document-directory"
// Save data to file
let fileName = "Test"
let url = uc.url
//let DocumentDirURL = URL(fileURLWithPath: "/Users/f/d/t/App/Assets/Apps/TestApp")
let DocumentDirURL = try! URL(resolvingAliasFileAt: url!)
let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("txt")
print("FilePath: \(fileURL.path)")
let writeString = "Write this text to the fileURL as text in iOS using Swift"
do {
// Write to the file
try writeString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}
var fullString: String = "" // Used to store the file contents
do {
// Read the file contents
fullString = try String(contentsOf: fileURL, encoding: .utf8)
} catch let error as NSError {
print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
}
print("File Text: \(readString)")
}
If I run the code as shown, he always gives me the error
"smb scheme is not supported" and then some additional errors that he can not write/read the file because he can not access it.
When I change the code and only search on the device I am programming on and run the simulator to search for this file everything works fine. So I have problems with "smb".
Thank you for every helpful answer.
you can use amsmb2 library to do this
you can extend the template class provided to connect to download files, write files, list directories -> on an smb share
everything is asynchronous from memory, with the librarys calls including hooks for progress updates on the ui main thread etc
i believe the amsmb2 library function your after might be uploadItem
iOS 13 includes SMB (server message block protocol) support
https://9to5mac.com/2019/06/17/ios-13-beta-2-enables-smb-server-connectivity-in-the-files-app/

How to solve Core Data error: Attempt to add read-only file at path when trying to access Core Data container in the app extension today widget?

I am trying to access a database using Core Data inside a today extension widget.
The function that creates the Core Data container is this:
func createContainer(completion: #escaping (NSPersistentContainer) -> ()) {
let applicationGroupIdentifier = "group.com.name.exampleApp.sharedContainer"
guard let newURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: applicationGroupIdentifier)
else { fatalError("Could not create persistent store URL") }
let persistentStoreDescription = NSPersistentStoreDescription()
persistentStoreDescription.url = newURL
let container = NSPersistentContainer(name: "ContainerName")
container.persistentStoreDescriptions = [persistentStoreDescription]
container.loadPersistentStores { (_, error) in
guard error == nil else { fatalError("Failed to load store:\(String(describing: error))") }
DispatchQueue.main.async { completion(container) }
}
}
However, I get the following error:
CoreData: error: Attempt to add read-only file at path
file:///private/var/mobile/Containers/Shared/AppGroup/C9324B65B-265C-4264-95DE-B5AC5C9DAFD0/
read/write. Adding it read-only instead. This will be a hard error in
the future; you must specify the NSReadOnlyPersistentStoreOption.
If I specify the NSReadOnlyPersistentStoreOption like this:
persistentStoreDescription.isReadOnly = false
I get the error:
Failed to load store:Optional(Error Domain=NSCocoaErrorDomain Code=513
"The file couldn’t be saved because you don’t have permission."):
What might be the cause and the solution?
Had the same problem and solved it by adding
let directory = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.identifier")!
let url = directory.appendingPathComponent("MyDatabaseName.sqlite")
CoreData probably tried to open the base folder as its sqlite database, which did not go so well.
Adding isReadOnly didn't allow CoreData to repurpose the Folder either :)
Source: https://github.com/maximbilan/iOS-Shared-CoreData-Storage-for-App-Groups
In extension's info.plist set: NSExtension --> NSExtensionAttributes --> RequestOpenAccess --> YES
In Settings on device set: Your app --> (Extension type - eg.: Keyboard) --> Allow Full Access

Reading from ApplicationSupportDirectory in iOS

Using LXLogger I am able to write log entries to file on the device and simulator (in addition to xcode console etc).
The default location which LXLogger stores the file is under the ApplicationSupportDirectory.
LXLogger writes to the file OK, and I can see the file is on my device (or on my mac HDD when using the simulator), but when I try to open the file I am told that the file does not exist.
I have added the error logs generated in line with the code.
I've tried a dozen different methods and multiple questions on here, to no avail.
let directory = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask).first!.URLByAppendingPathComponent(NSBundle.mainBundle().bundleIdentifier!, isDirectory: true).URLByAppendingPathComponent("logs", isDirectory: true)
do {
let array = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(directory, includingPropertiesForKeys: nil, options:[])
var str = array[0].description
str = str.stringByReplacingOccurrencesOfString("file://", withString: "", options: .LiteralSearch, range: nil)
let fileURL = NSURL(fileURLWithPath:str)
log.info("fileURL contains: " + fileURL.absoluteString)
do {
log.info("Attempting to get contentsOfURL")
let data = try NSString(contentsOfFile: array[0].description,
encoding: NSASCIIStringEncoding)
logTextView.text = data as String
} catch let error as NSError {
log.error(error.description)
}
}
catch let error as NSError {
log.error(error.description)
}
Log Outputs from above code:
> 2016-03-31 21:43:52.121 [INFO] fileURL contains: file:///private/var/mobile/Containers/Data/Application/2F812C06-E204-4543-905C-F884C35DA198/Library/Application%2520Support/myname.myapp/logs/1_log.txt
> 2016-03-31 21:43:52.122 [INFO] Attempting to get contentsOfURL
> 2016-03-31 21:43:52.123 [ERROR] Error Domain=NSCocoaErrorDomain Code=260 "The file “1_log.txt” couldn’t be opened because there is no such file." UserInfo={NSFilePath=file:///private/var/mobile/Containers/Data/Application/2F812C06-E204-4543-905C-F884C35DA198/Library/Application%20Support/myname.myapp/logs/1_log.txt, NSUnderlyingError=0x1376e0e00 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
So the code shows that the file is there (and i can physically open the copy on my mac), but the app can't find it.
Presumably its something to do with my usage of the file handle/nsurl/something like that??
Your approach of taking the NSURL from array and using stringByReplacingOccurrencesOfString and fileURLWithPath and then description is wrong; it doesn't correctly handle the percent-encoding in the URL and it's generally complex/fragile.
This can be much simpler, because you already have a file URL returned in the array from contentsOfDirectoryAtURL. You can just use it directly:
let data = try String(contentsOfURL: array[0], encoding: NSUTF8StringEncoding)

iOS Dropbox error loading thumbnails

I wanna get thumbnails for dropbox files that I display on a view. I know that I've to use the loadThumbnail method, but I don't get exactly how to do it.
I wrote this :
for file in dropboxMetadata.contents {
dbRestClient.loadThumbnail(file.path, ofSize: "s", intoPath: "https://api-content.dropbox.com/1/thumbnails/auto/")
}
but I get some errors like this :
error making request to /1/thumbnails/dropbox/star.jpg - (4) Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed.
Thanks for your help !
Petesh has the right idea. intoPath is the destination directory for those thumbnails.
Try this:
func createTempDirectory() -> String? {
let tempDirectoryTemplate = NSTemporaryDirectory().stringByAppendingPathComponent("XXXXX")
let fileManager = NSFileManager.defaultManager()
var err: NSErrorPointer = nil
// remove any previous temporary folder that's there, in case it's there
fileManager.removeItemAtPath(tempDirectoryTemplate)
if fileManager.createDirectoryAtPath(tempDirectoryTemplate, withIntermediateDirectories: true, attributes: nil, error: err) {
return tempDirectoryTemplate
} else {
print("can't create temporary directory at \(tempDirectoryTemplate)")
return nil
}
}
The above code for which I found in this question
Then you could change your own code to do something like:
let temporaryDirectory = createTempDirectory()
for file in dropboxMetadata.contents {
dbRestClient.loadThumbnail(file.path, ofSize: "s", intoPath: temporaryDirectory)
}
If this works, then you could change the "intoPath" parameter into any directory you think is more appropriate.

How to find a correct path in Swift

I got this code from the documentation, I can't find the path to my file, I need to copy the "contacts.db" file from Supporting Files Folder to the app in the device not in the simulator for offline use.
func copyItemAtPath(_ srcPath: String,
toPath dstPath: String,
error error: NSErrorPointer) -> Bool
srcPath = The path to the file or directory you want to move. This parameter must not be nil.
dstPath = The path at which to place the copy of srcPath. This path must include the name of the file or directory in its new location. This parameter must not be nil.
error = On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.
Any help is highly appreciated. :)
You can just drag and drop "contacts.db" into Project Navigator and after that you can find a path of that file this way:
let sourcePath = NSBundle.mainBundle().pathForResource("contacts", ofType: "db")
after that you can copy that file into document folder of your app and for that you need that document folder path:
let doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
let destinationPath = doumentDirectoryPath.stringByAppendingPathComponent("contacts1.db")
now you have sourcePath and destinationPath so you can copy that file into document folder:
NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath, error: &error)
and if you want to check for error you can do it this way:
var error : NSError?
NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath, error: &error)
if let error = error {
println(error.description)
}
If you are getting your sourcePath nil then goto Targets->YouApp->Build Phases->copy Bundle Resources and add your file there.
Hope this will help.

Resources