Error Domain = NSOSStatusErrorDomain Code = -5000,使用Swift上的setResourceValue

时间:2017-11-16 20:53:28

标签: swift

我的问题很简单我试图使用Swift向Finder中的某些文件添加标签,我遇到以下错误:Error Domain=NSOSStatusErrorDomain Code=-5000 "afpAccessDenied: Insufficient access privileges for operation "

这是我的职责:

    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)
        }
    }

也许有人可以帮助我?

2 个答案:

答案 0 :(得分:1)

问题似乎是,我通过xcode启动的应用程序使用沙盒模式。在Capabilities中将其关闭后,一切都像魅力一样。

答案 1 :(得分:0)

错误消息表示您无权访问这些文件。

如果您需要询问用户管理员权限,可以查看LocalAuthentication framework(适用于iOS 8+和MacOS 10.10 +)。

example使用deviceOwnerAuthenticationWithBiometrics 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 } ,需要TouchID或FaceID。这是允许用户使用其密码进行身份验证的相同示例:

<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js" integrity="sha384-pvXSwSU09c+q9mPyY++ygUHWIYRoaxgnJ/JC5wcOzMb/NVVu+IDniiB9qWp3ZNWM" crossorigin="anonymous"></script>