簡體   English   中英

使用 Swift 和 iOS 保存文件

[英]Save file using Swift and iOS

我有一個 iOS 應用程序,我需要能夠在我的 iCloud Drive 上選擇一個文件,修改該文件,並使用新的擴展名保存修改后的文件。 我已經嘗試了很多東西,但我仍然無法編寫新文件。 這是我的最新代碼:

documentPickerViewController = DocumentPickerViewController(documentTypes: ["public.item"], in: .open)  
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {  
    let fileURL = urls[0]  
    do {  
        // Read the file       
        _ = fileURL.startAccessingSecurityScopedResource()  
        let fileData = try Data(contentsOf: fileURL, options: .uncached)  
        fileURL.stopAccessingSecurityScopedResource()  

        // write the file  
        let fileCopyURL = fileURL.appendingPathExtension("copy")  
        _ = fileCopyURL.startAccessingSecurityScopedResource()  
        try fileData.write(to: fileCopyURL)  
        fileCopyURL.stopAccessingSecurityScopedResource()  
    }  
    catch {  
        print(error.localizedDescription)  
    }  
}  

當我在 iCloud Drive 上選擇文件時,出現以下錯誤:

您無權將“TestFile.txt.copy”文件保存在“Test Files”文件夾中。

如何保存修改后的文件?

首先,我認為您應該確保您擁有正確的權利/權限來寫入 iCloud Drive: Xcode 截圖

如果這仍然不起作用,請嘗試更新您的應用程序的CFBundleVersion或 Build Number,並確保您的 Bundle Identifier(在Signing & Capabilities的第一部分中)在您的 Apple Developer 帳戶中正確注冊。

根據您的代碼,您要完成的工作是可行的,但問題來自其他地方。

如果您在執行這些步驟時遇到問題,請告訴我。 祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM