{"id":7014,"date":"2025-05-30T12:58:45","date_gmt":"2025-06-05T05:57:44","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7014"},"modified":"2025-06-05T12:58:45","modified_gmt":"2025-06-05T05:57:44","slug":"https-badgameshow-com-steven-183","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-183\/","title":{"rendered":"Swift \u8a02\u55ae\u72c0\u614b\u67e5\u8a62 \ud83d\ude9a \u6700\u65b0\u6280\u8853\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, \u8a02\u55ae\u72c0\u614b\u67e5\u8a62, \u8a02\u55ae\u9001\u9054, URLSession, Swift \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h1>Swift \u8a02\u55ae\u72c0\u614b\u67e5\u8a62 \ud83d\ude9a \u6700\u65b0\u6280\u8853\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728\u73fe\u4ee3\u7db2\u8def\u6642\u4ee3\uff0c\u8a02\u55ae\u72c0\u614b\u67e5\u8a62\u662f\u4e00\u9805\u95dc\u9375\u529f\u80fd\uff0c\u5e6b\u52a9\u6d88\u8cbb\u8005\u96a8\u6642\u638c\u63e1\u8a02\u55ae\u72c0\u614b\u53ca\u9001\u9054\u9032\u5ea6\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 **Swift 5.7** \u4e2d\u7684 <code>URLSession<\/code> \u5be6\u73fe\u8a02\u55ae\u72c0\u614b\u67e5\u8a62\uff0c\u4e26\u6db5\u84cb\u932f\u8aa4\u6392\u9664\u53ca\u64f4\u5c55\u61c9\u7528\u7684\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4f7f\u7528 URLSession \u67e5\u8a62\u8a02\u55ae\u72c0\u614b<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b <code>URLSession<\/code> \u5c0d\u8c61\uff0c\u4e26\u9078\u64c7\u5408\u9069\u7684\u914d\u7f6e\u4f86\u78ba\u4fdd\u8acb\u6c42\u7684\u6548\u7387\u548c\u5b89\u5168\u6027\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let configuration = URLSessionConfiguration.default\nlet session = URLSession(configuration: configuration)\n<\/code><\/pre>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u9700\u8981\u8a2d\u5b9a\u4e00\u500b <code>URLRequest<\/code> \u4f86\u9032\u884c HTTP \u8acb\u6c42\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">guard let url = URL(string: \"https:\/\/www.example.com\/order\/status\") else {\n    print(\"Invalid URL\")\n    return\n}\nvar request = URLRequest(url: url)\nrequest.httpMethod = \"GET\"\n<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u4f7f\u7528 <code>dataTask<\/code> \u767c\u9001\u8acb\u6c42\u4e26\u8655\u7406\u4f3a\u670d\u5668\u56de\u61c9\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let task = session.dataTask(with: request) { data, response, error in\n    if let error = error {\n        print(\"Error: \\(error.localizedDescription)\")\n        return\n    }\n    \n    guard let data = data else {\n        print(\"No data received\")\n        return\n    }\n\n    \/\/ \u89e3\u6790\u56de\u61c9\u8cc7\u6599\n    do {\n        if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {\n            let status = json[\"status\"] as? String ?? \"\u672a\u77e5\u72c0\u614b\"\n            DispatchQueue.main.async {\n                self.statusLabel.text = status\n            }\n        }\n    } catch {\n        print(\"JSON parsing error: \\(error.localizedDescription)\")\n    }\n}\ntask.resume()\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4e0d\u50c5\u7372\u53d6\u4e86\u8cc7\u6599\uff0c\u9084\u9032\u884c\u4e86\u932f\u8aa4\u8655\u7406\u4ee5\u78ba\u4fdd\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u3002\u82e5\u4f3a\u670d\u5668\u8fd4\u56de\u7684\u8cc7\u6599\u7121\u6cd5\u89e3\u6790\uff0c\u6211\u5011\u6703\u6253\u5370\u76f8\u61c9\u7684\u932f\u8aa4\u8a0a\u606f\u3002<\/p>\n<h2>\u986f\u793a\u8a02\u55ae\u72c0\u614b<\/h2>\n<p>\u7576\u6211\u5011\u6210\u529f\u7372\u53d6\u4e26\u89e3\u6790\u8a02\u55ae\u72c0\u614b\u5f8c\uff0c\u63a5\u4e0b\u4f86\u7684\u6b65\u9a5f\u662f\u5c07\u72c0\u614b\u986f\u793a\u7d66\u7528\u6236\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">DispatchQueue.main.async {\n    self.statusLabel.text = status\n}\n<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u6211\u5011\u5c31\u5b8c\u6210\u4e86\u8a02\u55ae\u72c0\u614b\u67e5\u8a62\u7684\u57fa\u672c\u529f\u80fd\u3002\u6839\u64da\u9700\u6c42\uff0c\u6211\u5011\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u529f\u80fd\uff0c\u4f8b\u5982\u52a0\u5165\u8a02\u55ae\u6b77\u53f2\u67e5\u8a62\u6216\u63a8\u9001\u901a\u77e5\u7b49\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u6280\u5de7<\/h2>\n<p>1. **\u78ba\u8a8d URL \u7684\u6709\u6548\u6027**\uff1a\u78ba\u4fdd\u4f60\u4f7f\u7528\u7684 URL \u662f\u6b63\u78ba\u7684\u3002<br \/>\n2. **\u6aa2\u67e5\u7db2\u8def\u9023\u63a5**\uff1a\u5728\u9032\u884c\u8acb\u6c42\u524d\uff0c\u78ba\u8a8d\u8a2d\u5099\u662f\u5426\u6709\u7db2\u8def\u9023\u63a5\u3002<br \/>\n3. **\u8655\u7406 JSON \u89e3\u6790\u932f\u8aa4**\uff1a\u78ba\u4fdd\u4f3a\u670d\u5668\u8fd4\u56de\u7684\u8cc7\u6599\u683c\u5f0f\u6b63\u78ba\uff0c\u4e26\u5305\u542b\u9810\u671f\u7684\u9375\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528\u7bc4\u4f8b<\/h2>\n<p>\u4f60\u53ef\u4ee5\u5c07\u9019\u500b\u67e5\u8a62\u529f\u80fd\u61c9\u7528\u5230\u66f4\u8907\u96dc\u7684\u7cfb\u7d71\u4e2d\uff0c\u4f8b\u5982\u8207\u7528\u6236\u8cc7\u6599\u5eab\u7d50\u5408\uff0c\u6839\u64da\u7528\u6236 ID \u67e5\u8a62\u7279\u5b9a\u8a02\u55ae\u7684\u72c0\u614b\uff0c\u6216\u662f\u5be6\u73fe\u5373\u6642\u901a\u77e5\u529f\u80fd\uff0c\u7576\u8a02\u55ae\u72c0\u614b\u8b8a\u66f4\u6642\u5373\u6642\u544a\u77e5\u7528\u6236\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u8a02\u55ae\u72c0\u614b-\ud83d\ude9a\u8a02\u55ae\u9001\u9054\u72c0\u614b\u67e5\u8a62_1675400630.771948.webp\" alt=\"Swift \u8a02\u55ae\u72c0\u614b\u67e5\u8a62\" title=\"Swift \u8a02\u55ae\u72c0\u614b\u67e5\u8a62\u793a\u7bc4\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5b78\u7fd2\u4e86\u5982\u4f55\u4f7f\u7528 **Swift 5.7** \u4e2d\u7684 <code>URLSession<\/code> \u4f86\u67e5\u8a62\u8a02\u55ae\u72c0\u614b\uff0c\u4e26\u5c07\u7d50\u679c\u5b89\u5168\u5730\u986f\u793a\u7d66\u7528\u6236\u3002\u638c\u63e1\u9019\u500b\u6280\u80fd\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u63d0\u5347\u61c9\u7528\u7684\u7528\u6236\u9ad4\u9a57\u548c\u529f\u80fd\u6027\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u8655\u7406 URLSession \u7684\u932f\u8aa4\uff1f<\/h3>\n<p>\u5728 dataTask \u7684\u56de\u8abf\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6aa2\u67e5 error \u53c3\u6578\uff0c\u4e26\u6839\u64da\u932f\u8aa4\u985e\u578b\u9032\u884c\u76f8\u61c9\u7684\u8655\u7406\u3002<\/p>\n<h3>2. \u6211\u53ef\u4ee5\u4f7f\u7528 URLSession \u9032\u884c POST \u8acb\u6c42\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u53ea\u9700\u5c07 request.httpMethod \u8a2d\u70ba &#8220;POST&#8221;\uff0c\u4e26\u5728 request \u4e2d\u6dfb\u52a0 request.httpBody\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728 Swift \u4e2d\u89e3\u6790 JSON\uff1f<\/h3>\n<p>\u4f7f\u7528 <code>JSONSerialization<\/code> \u6216 <code>Codable<\/code> \u5354\u8b70\u4f86\u89e3\u6790 JSON \u6578\u64da\uff0c\u6839\u64da\u9700\u8981\u9078\u64c7\u5408\u9069\u7684\u65b9\u6cd5\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):\u4e86\u89e3Swift\u8a02\u55ae\u72c0\u614b\ud83d\ude9a\u8a02\u55ae\u9001\u9054\u72c0\u614b\u67e5\u8a62\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u67e5\u8a62\u8a02\u55ae\u72c0\u614b\uff0c\u4ee5\u53ca\u5982\u4f55\u6aa2\u67e5\u8a02\u55ae\u662f\u5426\u5df2\u7d93\u9001\u9054\u3002<\/p>\n","protected":false},"author":1,"featured_media":7013,"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-7014","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-\u8a02\u55ae\u72c0\u614b-\ud83d\ude9a\u8a02\u55ae\u9001\u9054\u72c0\u614b\u67e5\u8a62_1675400630.771948.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1P8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7014","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=7014"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7014\/revisions"}],"predecessor-version":[{"id":13325,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7014\/revisions\/13325"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7013"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}