You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS应用写入iCloud的文件无法在Mac Finder/Windows iCloud应用中显示的问题求助

iOS应用写入iCloud的文件无法在Mac Finder/Windows iCloud应用中显示的问题求助

各位好,我遇到了一个棘手的iCloud同步问题:我的iOS应用可以正常将文件写入iCloud,还能在另一台iPhone上读取这些文件——这证明文件确实存在于iCloud中,但这些文件只对我的应用可见,完全不会出现在Mac Finder的iCloud分区或者Windows的iCloud应用里。所有设备和电脑都登录了同一个Apple ID,使用的也是同一版本的应用。

先和大家说明我的配置和代码细节:

iCloud容器与Info.plist配置

我在Info.plist里配置了Ubiquitous容器,相关代码如下:

<key>NSUbiquitousContainers</key>
<dict>
    <key>iCloud.com.{my domain}.{my app}</key>
    <dict>
        <key>NSUbiquitousContainerIsDocumentScopePublic</key>
        <true/>
        <key>NSUbiquitousContainerSupportedFolderLevels</key>
        <string>Any</string>
        <key>NSUbiquitousContainerName</key>
        <string>{my app}</string>
    </dict>
</dict>
<key>UIFileSharingEnabled</key>
<true/>

我特意把NSUbiquitousContainerIsDocumentScopePublic设为true,就是希望文件能被Finder这类外部工具访问到,但完全没达到预期效果。

应用内的iCloud路径逻辑

我在代码里是这样拼接iCloud文件路径的:

NSFileManager *fman = [NSFileManager defaultManager];
NSURL *urlDrive = [fman URLForUbiquityContainerIdentifier: nil];
NSURL *urlDocs = [urlDrive URLByAppendingPathComponent:@"Documents"];
NSURL *urlFile = [urlDocs URLByAppendingPathComponent: @"hello.txt"];

也就是把文件存在iCloud容器的Documents目录下。

核心读写代码

以下是我用来读写iCloud文件的核心代码:

写入iCloud的代码

void writeToiCloud(void) {
    NSFileManager *fman = [NSFileManager defaultManager];
    NSURL *urlDrive = [fman URLForUbiquityContainerIdentifier: nil];
    NSURL *urlDocs = [urlDrive URLByAppendingPathComponent:@"Documents"];
    
    if( ! [fman fileExistsAtPath: urlDocs.path] ) {
        // 按需创建Documents目录
        NSError *err00 = nil;
        @try {
            [fman createDirectoryAtURL: urlDocs withIntermediateDirectories:true attributes:nil error:&err00];
        } @catch (NSException *except) {
            NSLog(@"Exception creating directory %@", except);
        }
    }
    
    NSURL *urlFile = [urlDocs URLByAppendingPathComponent:@"hello.txt"];
    NSData *fdata = [@"Hello world" dataUsingEncoding: NSUTF8StringEncoding];
    NSError *errorReturn = nil;
    Boolean ret = [fdata writeToURL: urlFile options: NSDataWritingAtomic error: &errorReturn];
    NSLog(@"returned %1d, error=%@", ret?1:0, errorReturn); // 日志总是显示"1 error=(nil)"
}

从iCloud读取的代码

void readFromiCloud(void) {
    NSFileManager *fman = [NSFileManager defaultManager];
    NSURL *urlDrive = [fman URLForUbiquityContainerIdentifier: nil];
    NSURL *urlDocs = [urlDrive URLByAppendingPathComponent:@"Documents"];
    NSURL *urlFile = [urlDocs URLByAppendingPathComponent:@"hello.txt"];
    
    if([fman fileExistsAtPath: urlFile.path]) {
        NSString *fullFN = [localDocsDirectory() stringByAppendingPathComponent: @"hello-Local.txt"];
        NSURL *destURL = [NSURL fileURLWithPath: fullFN];
        @try {
            [fman copyItemAtURL: urlFile toURL:destURL error:&errReading];
            NSLog(@"file copied from iCloud to local");
        } @catch(NSException *exc) {
            NSLog(@"copyItem threw %@", exc);
        }
    } else {
        NSLog(@"file %@ DOES NOT EXIST!", urlFile);
    }
}

额外的同步异常问题

还有一个奇怪的现象:我用一台运行iOS 15.8.4的iPhone 7和一台运行iOS 18.3.2的iPhone 13测试时,iPhone 7写入的文件能被iPhone 13上的应用正常读取,但iPhone 13写入的文件,iPhone 7上的应用完全读不到,日志一直提示文件不存在。我等了好几个小时,两台设备全程联网,同步就是没反应。另外每次修改Info.plist后,我都更新了应用的版本号。

现在我有两个核心疑问,希望各位能帮忙解答:

  1. 明明已经设置了NSUbiquitousContainerIsDocumentScopePublic为true,为什么文件还是不能在Finder/Windows iCloud应用中显示?我需要调整哪些配置或代码?
  2. 为什么iOS 18设备写入的文件无法同步到iOS 15设备?这是系统版本兼容性问题吗?有没有解决办法?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 12:25:32