ios - 应用程序创建的文件夹/文件未显示在 iPhone 上的 "Files"中

标签 ios objective-c icloud

想知道是否有人可以帮助我。我有一个应用程序,我正在尝试将一些文件移动到 iCloud 中,以便它们显示在"file"中并云到其他设备。我一直在网上浏览大量资源来研究问题所在,但似乎没有任何帮助。
在我的应用项目中,我在功能中打开了 iCloud 文档。
在我的 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 因为它已经存在。此外,如果我尝试在文件上设置 Ubiquitous:NO,它会告诉我在文件未同步时无法将其设置为 no。
知道为什么我的应用程序文件夹和此文件没有显示在 iCloud 下的 FILES 文件夹中吗?
我增加了捆绑版本,这是我在其他地方看到的。什么也没做。
我究竟做错了什么?

最佳答案

这让我完全震惊;我不知道这是可能的。我将完整描述我的测试应用程序。它会看起来很像你的!
这是捆绑标识符:
enter image description here
这里是权利:
enter image description here
这是权利文本:

<?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>
这是应用程序委托(delegate):
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),并且我必须终止并重新启动 Files 应用程序。然后我看到了我的文件(并且可以打开并检查它):
enter image description here

关于ios - 应用程序创建的文件夹/文件未显示在 iPhone 上的 "Files"中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65307694/

相关文章:

ios - 将主机名字符串解析为 IP 字符串

ios - Facebook iOS SDK-移动应用程序安装跟踪不起作用

ios - 在 iPad 上运行时,iPhone 应用程序 View 在底部被切断

ios - 我如何“通过电子邮件查找我”

ios - iCloud低速检索数据

php - iCloud CalDAV 通过 PHP

ios - Xcode IB Storyboard方向和容器 View

ios - 如何确保 iOS 6 中的 UIApplicationWillTerminateNotification?

ios - 为 Apple TV 2G 编写应用程序

iphone - 羽化 NSView 不透明度