{"id":6920,"date":"2025-11-22T15:33:38","date_gmt":"2025-11-22T07:33:38","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6920"},"modified":"2025-11-22T15:33:38","modified_gmt":"2025-11-22T07:33:38","slug":"%e6%b2%99%e7%9b%92%e6%96%87%e4%bb%b6%e7%a7%bb%e5%8a%a8%e5%88%b0-icloud-document","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6920","title":{"rendered":"\u6c99\u76d2\u6587\u4ef6\u79fb\u52a8\u5230 iCloud Document"},"content":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4e2a <strong>\u5168\u9762\u3001\u53ef\u76f4\u63a5\u843d\u5730\u7684<\/strong>\u300c\u6c99\u76d2\u6587\u4ef6 \u2192 iCloud Document\u300d\u79fb\u52a8\/\u8fc1\u79fb\u64cd\u4f5c\u6e05\u5355\uff0c\u5305\u62ec\u9700\u8981\u7684 API\u3001\u6ce8\u610f\u4e8b\u9879\u3001\u5143\u6570\u636e\u540c\u6b65\u3001\u8fdb\u5ea6\u5904\u7406\u7b49\u3002\u786e\u4fdd\u4f60\u80fd\u5728\u81ea\u5df1\u7684\u6587\u4ef6\u7ba1\u7406 App \u4e2d\u6b63\u786e\u5b9e\u73b0\u3002<\/p>\n<hr \/>\n<h1>\u2705 \u4e00\u3001\u6d89\u53ca\u7684\u76ee\u5f55<\/h1>\n<h3>1. <strong>\u6c99\u76d2\u76ee\u5f55\uff08App \u5185\u90e8\uff09<\/strong><\/h3>\n<p>\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><code>Documents\/<\/code><\/li>\n<li><code>Library\/<\/code><\/li>\n<li><code>tmp\/<\/code><\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u90fd\u53ea\u80fd\u672c\u5730\u8bbf\u95ee\u3002<\/p>\n<h3>2. <strong>iCloud Document\uff08\u57fa\u4e8e UIDocument \/ NSFileManager\uff09<\/strong><\/h3>\n<p>\u901a\u5e38\u662f\uff1a<\/p>\n<pre><code class=\"language-swift\">let ubiquityURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?\n    .appendingPathComponent(&quot;Documents&quot;)<\/code><\/pre>\n<p>\u7528\u6237\u7684 iCloud \u4f1a\u81ea\u52a8\u540c\u6b65\u8be5\u76ee\u5f55\u3002<\/p>\n<hr \/>\n<h1>\u2705 \u4e8c\u3001\u79fb\u52a8\u6587\u4ef6\u7684\u672c\u8d28\u6b65\u9aa4<\/h1>\n<blockquote>\n<p><strong>\u6c99\u76d2 \u2192 iCloud<\/strong> \u672c\u8d28\u662f\u4e24\u4e2a\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\u62f7\u8d1d\/\u79fb\u52a8\u672c\u5730\u6587\u4ef6\u5230 Ubiquity Container<\/li>\n<li>\u8ba9 iCloud \u63a5\u7ba1\u8be5\u6587\u4ef6\u5e76\u540c\u6b65<\/li>\n<\/ol>\n<\/blockquote>\n<hr \/>\n<h1>\u2699\ufe0f <strong>\u6b65\u9aa4 1\uff1a\u83b7\u53d6 iCloud Document \u76ee\u5f55 URL<\/strong><\/h1>\n<pre><code class=\"language-swift\">guard let containerURL = FileManager.default.url(\n    forUbiquityContainerIdentifier: nil\n)?.appendingPathComponent(&quot;Documents&quot;) else {\n    print(&quot;iCloud \u672a\u542f\u7528&quot;)\n    return\n}\n\ntry FileManager.default.createDirectory(\n    at: containerURL,\n    withIntermediateDirectories: true,\n    attributes: nil\n)<\/code><\/pre>\n<hr \/>\n<h1>\u2699\ufe0f <strong>\u6b65\u9aa4 2\uff1a\u6267\u884c\u6587\u4ef6\u79fb\u52a8\/\u590d\u5236<\/strong><\/h1>\n<h3>\ud83d\udd39 <strong>\u63a8\u8350\uff1acopy \u2192 remove\uff08\u6bd4 move \u66f4\u7a33\u5b9a\uff09<\/strong><\/h3>\n<p>Apple \u5b98\u65b9\u4e0d\u5efa\u8bae\u76f4\u63a5 move\uff0c\u56e0\u4e3a iCloud \u76ee\u5f55\u662f\u7279\u6b8a\u6587\u4ef6\u7cfb\u7edf\u3002<\/p>\n<pre><code class=\"language-swift\">let source = localURL        \/\/ \u6c99\u76d2\u6587\u4ef6\nlet destination = containerURL.appendingPathComponent(source.lastPathComponent)\n\ndo {\n    try FileManager.default.copyItem(at: source, to: destination)\n\n    \/\/ \u62f7\u8d1d\u6210\u529f\u540e\u5220\u9664\u672c\u5730\n    try FileManager.default.removeItem(at: source)\n\n} catch {\n    print(&quot;\u79fb\u52a8\u5931\u8d25\uff1a\\(error)&quot;)\n}<\/code><\/pre>\n<hr \/>\n<h1>\u2699\ufe0f <strong>\u6b65\u9aa4 3\uff1a\u542f\u52a8 iCloud \u5bf9\u6587\u4ef6\u7684\u7ba1\u7406<\/strong><\/h1>\n<p>\u5bf9\u4e8e Ubiquity \u6587\u4ef6\uff0ciCloud \u4f1a\u81ea\u52a8\u540c\u6b65\uff0c\u4f46\u4f60\u53ef\u4ee5\u624b\u52a8\u89e6\u53d1\uff1a<\/p>\n<pre><code class=\"language-swift\">var values = URLResourceValues()\nvalues.isExcludedFromBackup = false\ntry? destination.setResourceValues(values)<\/code><\/pre>\n<hr \/>\n<h1>\u2699\ufe0f <strong>\u6b65\u9aa4 4\uff1a\u76d1\u542c iCloud \u540c\u6b65\u72b6\u6001\uff08\u53ef\u9009\uff09<\/strong><\/h1>\n<p>\u76d1\u542c <code>NSMetadataQueryDidUpdate<\/code> \u6216 <code>NSMetadataQueryDidFinishGathering<\/code>\uff1a<\/p>\n<pre><code class=\"language-swift\">query = NSMetadataQuery()\nquery.predicate = NSPredicate(format: &quot;%K like &#039;*&#039;&quot;, NSMetadataItemFSNameKey)\nquery.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope]<\/code><\/pre>\n<p>\u5728\u56de\u8c03\u91cc\u83b7\u53d6\u6587\u4ef6\u72b6\u6001\uff1a<\/p>\n<pre><code class=\"language-swift\">let status = item.value(forAttribute: NSMetadataUbiquitousItemDownloadingStatusKey) as? String<\/code><\/pre>\n<p>\u53ef\u8fd4\u56de\uff1a<\/p>\n<ul>\n<li><code>current<\/code><\/li>\n<li><code>downloaded<\/code><\/li>\n<li><code>notDownloaded<\/code><\/li>\n<\/ul>\n<hr \/>\n<h1>\u2699\ufe0f <strong>\u6b65\u9aa4 5\uff1a\u4e0a\u4f20\u8fdb\u5ea6\uff08iCloud \u4e0a\u4f20\uff09<\/strong><\/h1>\n<p>iCloud \u5bf9\u4e0a\u4f20\u6ca1\u6709 progress API\uff0c\u4f46\u4f60\u53ef\u4ee5\uff1a<\/p>\n<h3>\u2714 \u76d1\u542c <code>NSMetadataItemPercentUploadedKey<\/code><\/h3>\n<pre><code class=\"language-swift\">if let percent = item.value(forAttribute: NSMetadataUbiquitousItemPercentUploadedKey) as? Double {\n    print(&quot;\u4e0a\u4f20\u8fdb\u5ea6\uff1a\\(percent)&quot;)\n}<\/code><\/pre>\n<hr \/>\n<h1>\u2699\ufe0f <strong>\u6b65\u9aa4 6\uff1a\u51b2\u7a81\u5904\u7406<\/strong><\/h1>\n<p>\u5f53\u7528\u6237\u53e6\u4e00\u53f0\u8bbe\u5907\u7f16\u8f91\u4e86\u540c\u4e00\u6587\u4ef6\uff0c\u4f60\u4f1a\u6536\u5230\uff1a<\/p>\n<ul>\n<li><code>NSFileVersion<\/code> \u51b2\u7a81<\/li>\n<\/ul>\n<pre><code class=\"language-swift\">let unresolved = NSFileVersion.unresolvedConflictVersionsOfItem(at: url) ?? []\nfor version in unresolved {\n    version.isResolved = true\n}<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u8ba9 UIDocument \u81ea\u52a8\u5408\u5e76\u3002<\/p>\n<hr \/>\n<h1>\ud83d\udc49 \u5173\u952e\u63d0\u9192\uff08\u975e\u5e38\u91cd\u8981\uff09<\/h1>\n<h2>1. <strong>\u4e0d\u8981\u4f7f\u7528 FileManager.moveItem \u76f4\u63a5\u79fb\u52a8<\/strong><\/h2>\n<p>\u56e0\u4e3a\u6c99\u76d2 \u2192 iCloud \u662f\u4e0d\u540c\u6587\u4ef6\u7cfb\u7edf\uff0c\u6709\u65f6\u4f1a\u5931\u8d25\u6216\u5bfc\u81f4\u6587\u4ef6\u4e22\u5931\u3002<br \/>\n\u6b63\u786e\u7b56\u7565\uff1a<\/p>\n<p>\u2714 \u590d\u5236<br \/>\n\u2714 \u6821\u9a8c<br \/>\n\u2714 \u5220\u9664\u539f\u6587\u4ef6<\/p>\n<hr \/>\n<h2>2. <strong>\u5728 iCloud \u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u81ea\u52a8\u540c\u6b65\uff0c\u4e0d\u9700\u8981\u4f60\u4e0a\u4f20<\/strong><\/h2>\n<p>\u552f\u4e00\u8981\u505a\u7684\u4e8b\u5c31\u662f\u628a\u6587\u4ef6\u653e\u5230 iCloud Document \u6587\u4ef6\u5939\u91cc\u3002<\/p>\n<hr \/>\n<h2>3. <strong>\u5927\u6587\u4ef6\u590d\u5236\u8981\u7528\u540e\u53f0\u961f\u5217<\/strong><\/h2>\n<p>\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\uff1a<\/p>\n<pre><code class=\"language-swift\">DispatchQueue.global().async {\n    try? FileManager.default.copyItem(...)\n}<\/code><\/pre>\n<hr \/>\n<h2>4. <strong>\u4e0d\u8981\u628a\u6574\u4e2a\u76ee\u5f55\u79fb\u52a8\u5230 iCloud<\/strong><\/h2>\n<p>\u5efa\u8bae\u6587\u4ef6\u7ea7\u8fc1\u79fb\uff0c\u4e0d\u5efa\u8bae\u76ee\u5f55\u7ea7 move\u3002<\/p>\n<hr \/>\n<h1>\ud83d\udccc \u6700\u4f73\u5b9e\u8df5\u7684\u5b8c\u6574\u65b9\u6cd5\uff08\u53ef\u76f4\u63a5\u7528\uff09<\/h1>\n<pre><code class=\"language-swift\">func moveToICloud(from localURL: URL) throws -&gt; URL? {\n    guard let container = FileManager.default.url(\n        forUbiquityContainerIdentifier: nil\n    )?.appendingPathComponent(&quot;Documents&quot;) else {\n        return nil\n    }\n\n    try FileManager.default.createDirectory(at: container, withIntermediateDirectories: true)\n\n    let dest = container.appendingPathComponent(localURL.lastPathComponent)\n\n    \/\/ copy\n    try FileManager.default.copyItem(at: localURL, to: dest)\n\n    \/\/ delete local\n    try FileManager.default.removeItem(at: localURL)\n\n    return dest\n}<\/code><\/pre>\n<hr \/>\n<h1>\u5982\u679c\u4f60\u613f\u610f\uff0c\u6211\u53ef\u4ee5\u5e2e\u4f60\uff1a<\/h1>\n<p>\u2714 \u8bbe\u8ba1\u4f60\u6587\u4ef6\u7ba1\u7406 App \u7684 iCloud \u6587\u4ef6\u64cd\u4f5c\u67b6\u6784<br \/>\n\u2714 \u63d0\u4f9b\u4e0b\u8f7d\u3001\u4e0a\u4f20\u3001\u540c\u6b65\u3001\u51b2\u7a81\u3001\u8fdb\u5ea6\u7684\u5b8c\u6574\u4ee3\u7801<br \/>\n\u2714 \u4f18\u5316\u4f60\u7684 NSMetadataQuery<br \/>\n\u2714 \u505a\u6210\u53ef\u590d\u7528\u6a21\u5757\uff08Protocol + Manager\uff09<\/p>\n<p>\u4f60\u73b0\u5728\u60f3\u4e86\u89e3\u54ea\u4e2a\u90e8\u5206\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4e2a \u5168\u9762\u3001\u53ef\u76f4\u63a5\u843d\u5730\u7684\u300c\u6c99\u76d2\u6587\u4ef6 \u2192 iCloud Document\u300d\u79fb\u52a8\/\u8fc1\u79fb\u64cd\u4f5c\u6e05\u5355\uff0c\u5305\u62ec\u9700\u8981\u7684&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6920\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[],"class_list":["post-6920","post","type-post","status-publish","format-standard","hentry","category-135"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6920"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6920\/revisions"}],"predecessor-version":[{"id":6921,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6920\/revisions\/6921"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}