NSFileManager moveItem throws Error Code=513

前端 未结 1 419
栀梦
栀梦 2021-01-02 05:40

I\'m trying to move a file from a URL to another, however I get an error with code 513. I understand that this is a NSFileWriteNoPermissionError. I

相关标签:
1条回答
  • 2021-01-02 06:28

    I solved my issue, but it may be different to what you were experiencing: I still had issues when changing to copyItem. I'm adding my findings here as an answer in case anyone else stumbles upon this question as I did and it helps them out.

    My app loads custom files that can be downloaded from my website. With iOS 13's download manager, these are copied to the Downloads folder first, and can then be opened by my app. Yet my app was having permission errors when attempting to access these files in the same way as iOS 12.

    This answer to a different question clued me in. I need to call startAccessingSecurityScopedResource() and stopAccessingSecurityScopedResource() as follows:

    // source and destination are URLs
    if source.startAccessingSecurityScopedResource() {
        try FileManager.default.moveItem(at: source, to: destination)
    }
    source.stopAccessingSecurityScopedResource()
    

    The Apple documentation doesn't really suggest that this is new with iOS 13 but I think just the different approach to file downloading means sandboxing considerations need to be made.

    0 讨论(0)
提交回复
热议问题