Ernestas Šeputis
Ernestas Šeputis

Reputation: 31

Error Domain=NSOSStatusErrorDomain Code=-5000 using setResourceValue on Swift

my question would be simple I am trying to add tags to some files in Finder using Swift and I am experiencing following error: Error Domain=NSOSStatusErrorDomain Code=-5000 "afpAccessDenied: Insufficient access privileges for operation "

and here is my function to do that:

    for product in fetchedObjects
    {
        let url = NSURL(fileURLWithPath: product.fileURLString!)

        print(product.fileNr)

        do
        {
            var tags = [String]()

            tags += ["test"]
            try url.setResourceValue(tags, forKey: URLResourceKey.tagNamesKey)
        }
        catch let error as NSError
        {
            print(error)
        }
    }

Maybe somebody could help me?

Upvotes: 1

Views: 952

Answers (2)

Ernestas Šeputis
Ernestas Šeputis

Reputation: 31

The issue seemed to be that, my app launched via xcode was using Sandbox mode. After turning it off in Capabilities everything works like a charm.

Upvotes: 1

leanne
leanne

Reputation: 8749

The error message means that you don't have authorization to access the files.

If you need to ask the user for admin permissions, you can take a look at the LocalAuthentication framework (available in iOS 8+ and MacOS 10.10+).

The example uses an LAPolicy of deviceOwnerAuthenticationWithBiometrics, which requires TouchID or FaceID. Here's the same example allowing the user to authenticate with their password as well:

let myContext = LAContext()
let myLocalizedReasonString = <#String explaining why app needs authentication#>

var authError: NSError?
if #available(iOS 8.0, macOS 10.12.1, *) {
    if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
        myContext.evaluatePolicy(. deviceOwnerAuthentication, localizedReason: myLocalizedReasonString) { success, evaluateError in
            if success {
                // User authenticated successfully, take appropriate action
            } else {
                // User did not authenticate successfully, look at error and take appropriate action
            }
        }
    } else {
        // Could not evaluate policy; look at authError and present an appropriate message to user
    }
} else {
    // Fallback on earlier versions
}

Upvotes: 0

Related Questions