File rename failing with NSCocoaErrorDomain Code=513 on device - ios

I'm trying to rename a file (a FileWrapper containing a UIDocument subclass) as follows:
coordinationQueue.async {
var error: NSError? = nil
let fileCoordinator = NSFileCoordinator(filePresenter: self)
fileCoordinator.coordinate(readingItemAt: url, options: .withoutChanges,
writingItemAt: targetURL, options: .forMoving, error: &error) { fromURL, toURL in
do {
fileCoordinator.item(at: fromURL, willMoveTo: toURL)
try FileManager().moveItem(at: fromURL, to: toURL)
fileCoordinator.item(at: fromURL, didMoveTo: toURL)
// Success!
} catch {
// Handle error
}
}
if let error = error {
// Handle error
}
}
… but it's failing on the moveItem, with the following error:
Error Domain=NSCocoaErrorDomain Code=513 "“from_file” couldn’t be moved because you don’t have permission to access “My_folder”." UserInfo={NSSourceFilePathErrorKey=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/My_folder/from_file, NSUserStringVariant=(
Move
), NSDestinationFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/My_folder/to_file, NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/My_folder/from_file, NSUnderlyingError=0x1c1240ab0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
Interestingly, it works on fine on the simulator.
Can anybody help me out with this? (x-posted on Apple forums)

Related

The operation couldn’t be completed. (NSURLErrorDomain error -1011.)

I'm trying to upload an image to a FTP server using swift 4
I used FileProvider which is a library for Ftp connection iOS
anyway I'm checking the provider is reachable. and Im getting this error
The operation couldn’t be completed. (NSURLErrorDomain error -1011.)
this is the full error object:
Error Domain=NSURLErrorDomain Code=-1011 "(null)" UserInfo={NSErrorFailingURLKey=ftp://xxxx.xxxx.xxxx.xxxx:21/, NSErrorFailingURLStringKey=ftp://xxxx.xxxx.xxxx.xxxx:21/, NSURL=ftp://xxxx.xxxx.xxxx.xxxx:21/}
this is my code:
self.ftpProvider!.isReachable { (flag, error) in
if flag {
print("RIZK:--- provider is reachable ")
let progress = self.ftpProvider!.copyItem(localFile: localURL, to: "/files/\(imageName)", overwrite: true, completionHandler: { (error) in
if error == nil {
print("RIZK:-- Ready to upload")
}else{
print("RIZK:--\(error?.localizedDescription ?? "")")
}
})
print(progress?.fractionCompleted ?? 0)
}else{
print("RIZK:--- provider not reachable, with error \(error?.localizedDescription ?? "") ")
}
}

Checking if song is present in user's Spotify library

Using the below code to check if the user's library contains a particular song. It returns an error. What could I be doing wrong? I couldn't find examples for this particular 'forCheckingIfSavedTracksContains' method anywhere.
let accessToken = SPTAuth.defaultInstance().sessionUserDefaultsKey
let request: URLRequest = try!
SPTYourMusic.createRequest(forCheckingIfSavedTracksContains: ["spotify:track:3ZakaL0QEt5eeD3N7HbaN1"], forUserWithAccessToken: accessToken)
SPTRequest.sharedHandler().perform(request) { (error, response, data) in
if error == nil {
print ("this is data\(String(describing: data))")
} else {
print ("error")
}
}
The error is:
Error Domain=NSURLErrorDomain Code=-1012 "(null)" UserInfo={NSErrorFailingURLStringKey=api.spotify.com/v1/me/tracks/contains?ids=, NSUnderlyingError=0x109e7eb60 {Error Domain=kCFErrorDomainCFNetwork Code=-1012 "(null)" UserInfo={_kCFURLErrorAuthFailedResponseKey=<CFURLResponse 0x109e7e970 [0x1b441e538]>{url = api.spotify.com/v1/me/tracks/contains?ids=}}}, NSErrorFailingURLKey=api.spotify.com/v1/me/tracks/contains?ids=}

Don't have permission to access video file path in swift

Getting this error when uploading file to server
Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_0011.MOV” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Media/DCIM/100APPLE/IMG_0011.MOV, NSUnderlyingError=0x13b157510 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
My code for converting path to NSDATA is:
let videoURL = User.sharedInstance.arrRoomGalleryVideos.objectAtIndex(index) as? NSURL
var movieData: NSData?
do {
let video = try NSData(contentsOfURL: videoURL!, options: .DataReadingMappedIfSafe)
// print("video", video)
multipartFormData.appendBodyPart(data: video, name: "video_path[]", fileName: strVidName, mimeType: "mp4/.mov")
} catch {
print(error)
return
}
You can try this -
Navigate to Build Settings -> Go to Build Options and change the value of the "Compiler for C/C++/Objective-C" to Default Compiler.
For more solutions check this

NSFileManager fileExistsAtPath unable to overwrite existing file in Swift 2

Hello I'm upgrading my existing code to Swift 2, and I need some help with the copy of a file inside the DocumentDirectoy.
This is converted code I'm using to check if the file exists, and if it does, we should copy it either way, but it keeps returning an error saying the file exists, which it is true, but we need to overwrite it.
func copyXMLFile()
{
// Get a reference for the Document directory
let rootPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0]
// Get a reference for the data.xml file
xmlPathInDocument = rootPath.stringByAppendingString("data.xml")
if NSFileManager.defaultManager().fileExistsAtPath(xmlPathInDocument){
print("xml file exists")
let xmlPathInBundle = NSBundle.mainBundle().pathForResource("data", ofType: "xml")!
do {
// copy file from main bundle to documents directory
print("copy")
try NSFileManager.defaultManager().copyItemAtPath(xmlPathInBundle, toPath: xmlPathInDocument)
} catch let error as NSError {
// Catch fires here, with an NSErrro being thrown
print("error occurred, here are the details:\n \(error)")
}
}
else
{
// copy the file either way
let xmlPathInBundle = NSBundle.mainBundle().pathForResource("data", ofType: "xml")!
do {
// copy file from main bundle to documents directory
print("copy")
try NSFileManager.defaultManager().copyItemAtPath(xmlPathInBundle, toPath: xmlPathInDocument)
} catch let error as NSError {
// Catch fires here, with an NSErrro being thrown
print("error occurred, here are the details:\n \(error)")
}
}
}
error occurred, here are the details:
Error Domain=NSCocoaErrorDomain Code=516 "The operation couldn’t be
completed. (Cocoa error 516.)" UserInfo=0x7a67b680
{NSSourceFilePathErrorKey=/Users/User1/Library/Developer/CoreSimulator/Devices/0E591E5B-2E2F-4CCB-9099-95CE1EA3F557/data/Containers/Bundle/Application/E3C8FAE4-703D-46CA-AC37-A1C96A74E6BE/myApp.app/data.xml,
NSUserStringVariant=(
Copy ), NSFilePath=/Users/User1/Library/Developer/CoreSimulator/Devices/0E591E5B-2E2F-4CCB-9099-95CE1EA3F557/data/Containers/Bundle/Application/E3C8FAE4-703D-46CA-AC37-A1C96A74E6BE/myApp.app/data.xml,
NSDestinationFilePath=/Users/User1/Library/Developer/CoreSimulator/Devices/0E591E5B-2E2F-4CCB-9099-95CE1EA3F557/data/Containers/Data/Application/09F690B3-BDD8-4482-ADDE-E33F30D4B873/Documentsdata.xml,
NSUnderlyingError=0x7a67dd80 "The operation couldn’t be completed.
File exists"}
Please HELP!
Implement NSFileManagerDelegate and the delegate method
optional func fileManager(_ fileManager: NSFileManager,
shouldProceedAfterError error: NSError,
copyingItemAtURL srcURL: NSURL,
toURL dstURL: NSURL) -> Bool
and return true
Alternatively you could use
func replaceItemAtURL(_ originalItemURL: NSURL,
withItemAtURL newItemURL: NSURL,
backupItemName backupItemName: String?,
options options: NSFileManagerItemReplacementOptions,
resultingItemURL resultingURL: AutoreleasingUnsafeMutablePointer<NSURL?>) throws

plist attachment from email in swift (NS Cocoa Error Domain Code 260)

I am sending some ios file data via an email contained in a plist. No issues attaching it, I can check the file and the contents. It's all there and I can open it as a plist. Clicking on it (I have associated it with my app) it opens and I get a valid path:
file:///private/var/mobile/Containers/Data/Application/C5454580-2BEB-4515-9BDE-FED85FF54F76/Documents/Inbox/ShareStrength-11.bps
that I pass but when I try and read back the NSDictionary (plist) I get nil content.
let sourceFile = NSDictionary(contentsOfFile: URLString)
Any ideas on what is going wrong. Having difficulty debugging.
EDIT: I found some error code:
var error: NSError?
let content = NSString(contentsOfFile: URLString, encoding:NSUTF8StringEncoding, error: &error)
if content != nil
{
println("content: \(content)")
}
else
{
println("error: \(error)")
}
And I get the error:
error: Optional(Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x17eceeb0 {NSFilePath=file:///private/var/mobile/Containers/Data/Application/571DB0FF-6C5A-4BEB-9FA8-6E4DFE10E850/Documents/Inbox/ShareStrength-13.bps, NSUnderlyingError=0x17ee9810 "The operation couldn’t be completed. No such file or directory"})
Is this a sandbox issue? How can I copy the attachment being passed as a URL?
I was using:
let URLString: String = url.absoluteString!
instead of:
let URLString: String = url.path!
Hope this helps someone else

Resources