• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 获取有关 iCloud 中文件更改的通知

[复制链接]
菜鸟教程小白 发表于 2022-12-12 17:55:40 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在两个不同的设备上创建了一个名为 File - 1.jpg 的文件,并将其放入 iCloud 容器中。

我不使用 UIDocument,即使我尝试使用它,它也不会产生冲突。相反,我看到的是 iCloud 正在自动重命名和移动文档。

所以上传一个或另一个文件后变成File - 2.jpg。这一切都很好,但现在我没有对文件的引用,所以我不知道哪个是哪个...

有什么方法可以在应用程序端获得通知,文件在 iCloud 中被重命名/移动/删除?



Best Answer-推荐答案


最终,我必须创建一个实现 NSFilePresenter 的类并将其指向 iCloud 容器文件夹。

来自 iCloud 的实时更新可能会很晚,并且仅在 iCloud 提取元数据时才会发生。

此外,我必须将每个创建的文件与每个设备和 iCloud 帐户相关联并保留这些数据,在我的情况下是 CoreData。这就是 ubiquityIdentityToken 变得有用的地方。

iCloud 容器中的所有文件操作都应该使用 NSFileCoordinator 进行。

对于添加/删除事件,最好使用 NSMetadataQueryNSFileCoordinator 根本不会报告这些,但对于检测文件何时移动仍然很有用,这就是元数据查询报告为更新。

这是一个非常基本的样板,可以用作起点:

@interface iCloudFileCoordinator () <NSFilePresenter>

@property (nonatomic) NSString *containerID;
@property (nonatomic) NSURL *containerURL;

@property (nonatomic) NSOperationQueue *operationQueue;

@end

@implementation iCloudFileCoordinator

- (instancetype)initWithContainerIDNSString *)containerID {
    self = [super init];
    if(!self) {
        return nil;
    }

    self.containerID = containerID;
    self.operationQueue = [[NSOperationQueue alloc] init];
    self.operationQueue.qualityOfService = NSQualityOfServiceBackground;

    [self addFilePresenter];

    return self;
}

- (void)dealloc {
    [self removeFilePresenter];
}

- (void)addFilePresenter {
    [NSFileCoordinator addFilePresenter:self];
}

- (void)removeFilePresenter {
    [NSFileCoordinator removeFilePresenter:self];
}

#pragma mark - NSFilePresenter
#pragma mark - 

- (NSURL *)presentedItemURL {
    NSURL *containerURL = self.containerURL;

    if(containerURL) {
        return containerURL;
    }

    NSFileManager *fileManager = [[NSFileManager alloc] init];

    containerURL = [fileManager URLForUbiquityContainerIdentifier:self.containerID];

    self.containerURL = containerURL;

    return containerURL;
}

- (NSOperationQueue *)presentedItemOperationQueue {
    return self.operationQueue;
}

- (void)presentedSubitemAtURLNSURL *)oldURL didMoveToURLNSURL *)newURL {
    NSLog(@"Moved file from %@ to %@", oldURL, newURL);
}

/*
 ... and other bunch of methods that report on sub item changes ...
 */

@end

关于ios - 获取有关 iCloud 中文件更改的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34423110/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap