Folders/Files 创建的应用未显示在 iPhone 的 "Files" 中

App's created Folders/Files don't show up in "Files" on iPhone

想知道是否有人可以帮助我。我有一个应用程序,我正在尝试将一些文件移动到 iCloud 中,这样它们就会显示在“文件”中,并在云端显示到其他设备。我一直在浏览大量在线资源来研究问题所在,但似乎没有任何帮助。

在我的应用程序项目中,我在功能中打开了iCloud Documents。

在我的 plist 文件中,我有这个:

<key>NSUbiquitousContainers</key>
    <dict>
        <key>iCloud.com.mypublishername.myappname</key>
        <dict>
            <key>NSUbiquitousContainerIsDocumentScopePublic</key>
            <true/>
            <key>NSUbiquitousContainerName</key>
            <string>myappname</string>
            <key>NSUbiquitousContainerSupportedFolderLevels</key>
            <string>Any</string>
        </dict>
    </dict>

在我的权利文件中我有:

<dict>
    <key>com.apple.developer.icloud-container-identifiers</key>
    <array>
        <string>iCloud.com.mypublishername.myappname</string>
    </array>
    <key>com.apple.developer.icloud-services</key>
    <array>
        <string>CloudDocuments</string>
    </array>
    <key>com.apple.developer.ubiquity-container-identifiers</key>
    <array>
        <string>iCloud.com.mypublishername.myappname</string>
    </array>
</dict>

在 ObjC 中,我像这样获取 iCloud 文件夹:

NSURL *rootDirectory = [[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"];
if (rootDirectory)
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:rootDirectory.path isDirectory:nil]) [[NSFileManager defaultManager] createDirectoryAtURL:rootDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    gCloudFolder=rootDirectory;
}

然后,当我保存文件时,我会在本地保存,然后像这样将其移动到云文件夹中:

//
// theFilename is a file in the app's documents folder...
//
int aFile=creat(theFilename,S_IREAD|S_IWRITE);close(aFile);
aFile=open(theFilename,O_BINARY|O_RDWR);
if (aFile)
{
    write(aFile,theDataPtr,theLen);
    close(aFile);

    if (gCloudFolder)
    {
        NSURL *aLocalStr=[NSURL fileURLWithPath:[NSString stringWithUTF8String:theFilename]];
        NSURL *aCloudStr=[gCloudFolder URLByAppendingPathComponent:@"testing_file.txt"];
                
        NSError *error;
        if (![[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:aLocalStr destinationURL:aCloudStr error:&error]) NSLog(@"iCloud Error occurred: %@", error);
    }

那么...会发生什么。这个文件确实被创建了。如果我 运行 这两次,它告诉我它不能移动到 testing_file.txt 因为它已经存在。此外,如果我尝试 setUbiquitous:NO 文件,它告诉我当文件尚未同步时我不能将其设置为 no。

知道为什么我的应用文件夹和这个文件没有出现在 iCloud 下的 FILES 文件夹中吗?

我增加了捆绑版本,这是我在其他地方看到的。什么都没做。

我做错了什么?

这让我完全震惊了;我不知道这是可能的。我将完整描述我的测试应用程序。它会看起来很像你的!

这是包标识符:

权利如下:

权利文本如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.developer.icloud-container-identifiers</key>
    <array>
        <string>iCloud.com.neuburg.matt.SaveIntoFilesApp</string>
    </array>
    <key>com.apple.developer.icloud-services</key>
    <array>
        <string>CloudDocuments</string>
    </array>
    <key>com.apple.developer.ubiquity-container-identifiers</key>
    <array>
        <string>iCloud.com.neuburg.matt.SaveIntoFilesApp</string>
    </array>
</dict>
</plist>

这是 Info.plist 中的条目:

<key>NSUbiquitousContainers</key>
<dict>
    <key>iCloud.com.neuburg.matt.SaveIntoFilesApp</key>
    <dict>
        <key>NSUbiquitousContainerIsDocumentScopePublic</key>
        <true/>
        <key>NSUbiquitousContainerName</key>
        <string>MyApp</string>
        <key>NSUbiquitousContainerSupportedFolderLevels</key>
        <string>Any</string>
    </dict>
</dict>

这是应用委托:

var ubiq : URL!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    DispatchQueue.global(qos:.default).async {
        let fm = FileManager.default
        let ubiq = fm.url(forUbiquityContainerIdentifier:nil)
        print("ubiq: \(ubiq as Any)")
        DispatchQueue.main.async {
            self.ubiq = ubiq
        }
    }

    return true
}

这是我点击的按钮:

@IBAction func doButton (_ sender:Any) {
    if let del = UIApplication.shared.delegate as? AppDelegate {
        if let ubiq = del.ubiq {
            do {
                let fm = FileManager.default
                let docs = ubiq.appendingPathComponent("Documents")
                try? fm.createDirectory(at: docs, withIntermediateDirectories: false, attributes: nil)
                let url = docs.appendingPathComponent("test.txt")
                print("here we go")
                try? fm.removeItem(at: url)
                try "howdy \(Date())".write(to: url, atomically: true, encoding: .utf8)
                print("saved")
                
            } catch {
                print(error)
            }
        }
    }
}

我确实必须增加捆绑包版本(从 1 到 2),我确实必须终止并重新启动文件应用程序。然后我看到了我的文件(并且可以打开并检查它):