{"id":6800,"date":"2025-05-29T13:26:42","date_gmt":"2025-06-05T05:46:42","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6800"},"modified":"2025-06-05T13:26:42","modified_gmt":"2025-06-05T05:46:42","slug":"https-badgameshow-com-steven-76","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-76\/","title":{"rendered":"Swift \u80cc\u666f\u4e0b\u8f09\u6280\u5de7\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u80cc\u666f\u4e0b\u8f09, \u4e0b\u8f09\u6280\u5de7, URLSession, iOS \u958b\u767c, macOS \u958b\u767c\"><\/p>\n<h1>Swift \u80cc\u666f\u4e0b\u8f09\u6280\u5de7\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5feb\u901f\u3001\u5b89\u5168\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u958b\u767c iOS\u3001macOS\u3001watchOS \u548c tvOS \u61c9\u7528\u7a0b\u5f0f\u3002\u5728\u73fe\u4ee3\u61c9\u7528\u4e2d\uff0c\u80cc\u666f\u4e0b\u8f09\u662f\u4e00\u9805\u91cd\u8981\u6280\u8853\uff0c\u80fd\u5920\u8b93\u60a8\u7684\u61c9\u7528\u5728\u4e0d\u6253\u64fe\u4f7f\u7528\u8005\u7684\u60c5\u6cc1\u4e0b\u4e0b\u8f09\u5927\u578b\u6a94\u6848\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728 Swift \u4e2d\u5be6\u73fe\u80cc\u666f\u4e0b\u8f09\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u80cc\u666f\u4e0b\u8f09\uff1f<\/h2>\n<p>\u80cc\u666f\u4e0b\u8f09\u5141\u8a31\u61c9\u7528\u7a0b\u5f0f\u5728\u5f8c\u53f0\u57f7\u884c\u4e0b\u8f09\u4efb\u52d9\uff0c\u5373\u4f7f\u4f7f\u7528\u8005\u6b63\u5728\u4f7f\u7528\u5176\u4ed6\u529f\u80fd\uff0c\u4e5f\u4e0d\u6703\u5f71\u97ff\u4ed6\u5011\u7684\u9ad4\u9a57\u3002\u9019\u5c0d\u65bc\u5927\u578b\u6a94\u6848\u7684\u4e0b\u8f09\u5c24\u70ba\u91cd\u8981\uff0c\u5982\u8996\u983b\u3001\u97f3\u6a02\u6216\u61c9\u7528\u66f4\u65b0\u3002<\/p>\n<h2>\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528\u80cc\u666f\u4e0b\u8f09\uff1f<\/h2>\n<p>\u5728 Swift \u4e2d\u5be6\u73fe\u80cc\u666f\u4e0b\u8f09\u975e\u5e38\u7c21\u55ae\uff0c\u60a8\u53ea\u9700\u4f7f\u7528 <code>URLSession<\/code> \u985e\u5225\u7684 <code>downloadTask<\/code> \u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528\u6700\u65b0\u8a9e\u6cd5\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>import Foundation\n\nlet url = URL(string: \"https:\/\/example.com\/file.zip\")!\n\nlet configuration = URLSessionConfiguration.background(withIdentifier: \"com.example.backgroundDownload\")\nlet session = URLSession(configuration: configuration)\n\nlet task = session.downloadTask(with: url) { (location, response, error) in\n    guard let location = location, error == nil else {\n        print(\"\u4e0b\u8f09\u5931\u6557\uff1a\\(error?.localizedDescription ?? \"\u672a\u77e5\u932f\u8aa4\")\")\n        return\n    }\n    \/\/ \u8655\u7406\u4e0b\u8f09\u7684\u6a94\u6848\n    print(\"\u6a94\u6848\u4e0b\u8f09\u5230\uff1a\\(location)\")\n    \n    \/\/ \u4f8b\u5982\uff1a\u5c07\u6a94\u6848\u79fb\u52d5\u81f3\u61c9\u7528\u7a0b\u5f0f\u7684\u6587\u4ef6\u7cfb\u7d71\u4e2d\n    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!\n    let destinationURL = documentsDirectory.appendingPathComponent(\"file.zip\")\n\n    do {\n        try FileManager.default.moveItem(at: location, to: destinationURL)\n        print(\"\u6a94\u6848\u5df2\u6210\u529f\u79fb\u52d5\u5230\uff1a\\(destinationURL)\")\n    } catch {\n        print(\"\u79fb\u52d5\u6a94\u6848\u5931\u6557\uff1a\\(error.localizedDescription)\")\n    }\n}\ntask.resume()\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u900f\u904e <code>URLSessionConfiguration.background<\/code> \u65b9\u6cd5\u5275\u5efa\u4e86\u4e00\u500b\u80cc\u666f\u6703\u8a71\u914d\u7f6e\uff0c\u4e26\u4f7f\u7528 <code>downloadTask<\/code> \u65b9\u6cd5\u4f86\u4e0b\u8f09\u6a94\u6848\u3002\u7576\u4e0b\u8f09\u5b8c\u6210\u5f8c\uff0c\u56de\u547c\u51fd\u5f0f\u5c07\u88ab\u8abf\u7528\uff0c\u60a8\u53ef\u4ee5\u5728\u5176\u4e2d\u8655\u7406\u4e0b\u8f09\u7684\u6a94\u6848\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u9032\u884c\u80cc\u666f\u4e0b\u8f09\u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8655\u7406\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li>\u6aa2\u67e5 URL \u662f\u5426\u6709\u6548\u3002<\/li>\n<li>\u78ba\u4fdd\u61c9\u7528\u64c1\u6709\u7db2\u8def\u6b0a\u9650\u3002<\/li>\n<li>\u6aa2\u67e5\u4e0b\u8f09\u7684\u6a94\u6848\u5927\u5c0f\uff0c\u78ba\u4fdd\u4e0d\u6703\u8d85\u904e\u8a2d\u5099\u5b58\u5132\u9650\u5236\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u80cc\u666f\u4e0b\u8f09\u4e0d\u50c5\u50c5\u9069\u7528\u65bc\u55ae\u4e00\u6a94\u6848\u7684\u4e0b\u8f09\uff0c\u60a8\u53ef\u4ee5\u64f4\u5c55\u5176\u529f\u80fd\uff0c\u5be6\u73fe\u591a\u6a94\u6848\u7684\u540c\u6642\u4e0b\u8f09\u3001\u4e0b\u8f09\u9032\u5ea6\u7684\u8ffd\u8e64\u7b49\u529f\u80fd\uff0c\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528\u80cc\u666f\u4e0b\u8f09\u6280\u5de7\uff0c\u901a\u904e <code>URLSession<\/code> \u985e\u5225\u7684 <code>downloadTask<\/code> \u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\u5927\u578b\u6a94\u6848\u7684\u4e0b\u8f09\u800c\u4e0d\u5f71\u97ff\u4f7f\u7528\u8005\u64cd\u4f5c\u3002\u5e0c\u671b\u60a8\u80fd\u5920\u904b\u7528\u9019\u4e9b\u77e5\u8b58\uff0c\u63d0\u5347\u60a8\u61c9\u7528\u7684\u529f\u80fd\u3002<\/p>\n<p>\u5982\u9700\u66f4\u6df1\u5165\u4e86\u89e3\u80cc\u666f\u4e0b\u8f09\uff0c\u8acb\u53c3\u8003 Apple \u7684 <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/urlsession\/1411571-downloadtask\" target=\"_blank\" rel=\"noopener\">\u5b98\u65b9\u6587\u4ef6<\/a>\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u80cc\u666f\u4e0b\u8f09-\ud83d\udcbe-\u80cc\u666f\u4e0b\u8f09\u6280\u5de7_1675396007.727096.webp\" alt=\"Swift \u80cc\u666f\u4e0b\u8f09\u6280\u5de7\u793a\u4f8b\" title=\"Swift \u80cc\u666f\u4e0b\u8f09\u3001URLSession\u3001URLSessionDownloadTask\u3001iOS \u4e0b\u8f09\u3001macOS \u4e0b\u8f09\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u80cc\u666f\u4e0b\u8f09\u5982\u4f55\u5f71\u97ff\u61c9\u7528\u6027\u80fd\uff1f<\/h3>\n<p>\u80cc\u666f\u4e0b\u8f09\u4e0d\u6703\u5f71\u97ff\u61c9\u7528\u7684\u6027\u80fd\uff0c\u56e0\u70ba\u5b83\u5728\u5f8c\u53f0\u904b\u884c\uff0c\u4f7f\u7528\u8005\u53ef\u4ee5\u7e7c\u7e8c\u4f7f\u7528\u5176\u4ed6\u529f\u80fd\u3002<\/p>\n<h3>2. \u5982\u4f55\u8655\u7406\u4e0b\u8f09\u904e\u7a0b\u4e2d\u7684\u932f\u8aa4\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u901a\u904e\u6aa2\u67e5\u56de\u547c\u51fd\u5f0f\u4e2d\u7684 <code>error<\/code> \u53c3\u6578\u4f86\u8655\u7406\u932f\u8aa4\uff0c\u4e26\u6839\u64da\u5177\u9ad4\u60c5\u6cc1\u7d66\u4e88\u4f7f\u7528\u8005\u63d0\u793a\u6216\u91cd\u8a66\u4e0b\u8f09\u3002<\/p>\n<h3>3. \u662f\u5426\u53ef\u4ee5\u5728\u80cc\u666f\u4e0b\u8f09\u4e2d\u8ffd\u8e64\u9032\u5ea6\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>URLSessionDownloadTask<\/code> \u7684\u9032\u5ea6\u56de\u547c\u4f86\u5be6\u73fe\u4e0b\u8f09\u9032\u5ea6\u7684\u8ffd\u8e64\uff0c\u5f9e\u800c\u63d0\u4f9b\u66f4\u597d\u7684\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u5b78\u7fd2Swift\u80cc\u666f\u4e0b\u8f09\u7684\u6280\u5de7\uff0c\u53ef\u4ee5\u8b93\u4f60\u5728iOS\u548cmacOS\u4e0a\u66f4\u6709\u6548\u7387\u5730\u4e0b\u8f09\u6a94\u6848\uff0c\u4e26\u4e14\u53ef\u4ee5\u5728\u7a0b\u5f0f\u57f7\u884c\u6642\u66ab\u505c\u548c\u6062\u5fa9\u4e0b\u8f09\u3002\u672c\u6587\u5c07\u4ecb\u7d39Swift\u80cc\u666f\u4e0b\u8f09\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528URLSession\u548cURLSessionDownloadTask\u4f86\u5be6\u73fe\u80cc\u666f\u4e0b\u8f09\u3002<\/p>\n","protected":false},"author":1,"featured_media":6799,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-6800","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u80cc\u666f\u4e0b\u8f09-\ud83d\udcbe-\u80cc\u666f\u4e0b\u8f09\u6280\u5de7_1675396007.727096.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1LG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6800"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6800\/revisions"}],"predecessor-version":[{"id":13528,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6800\/revisions\/13528"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6799"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}