{"id":7348,"date":"2025-06-01T12:27:36","date_gmt":"2025-06-05T04:49:35","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7348"},"modified":"2025-06-05T12:27:36","modified_gmt":"2025-06-05T04:49:35","slug":"https-badgameshow-com-steven-345","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-345\/","title":{"rendered":"Swift \u7db2\u8def\u9023\u63a5\u6aa2\u6e2c\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, \u7db2\u8def\u9023\u63a5\u6aa2\u6e2c, Reachability, Swift 2025, \u7db2\u8def\u72c0\u614b\u6aa2\u6e2c\"><\/p>\n<h2>\u4ecb\u7d39 Swift \u7db2\u8def\u9023\u63a5\u6aa2\u6e2c<\/h2>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c\u6aa2\u6e2c\u7db2\u8def\u9023\u63a5\u72c0\u614b\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u529f\u80fd\u3002\u9019\u4e0d\u50c5\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u78ba\u4fdd\u61c9\u7528\u7a0b\u5f0f\u7684\u7a69\u5b9a\u6027\uff0c\u9084\u80fd\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\u3002\u5728 Swift \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>Reachability<\/strong> \u985e\u4f86\u8f15\u9b06\u6aa2\u6e2c\u7db2\u8def\u9023\u63a5\u72c0\u614b\u3002<\/p>\n<h2>\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 Reachability<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5c07 Reachability \u985e\u5225\u6dfb\u52a0\u5230\u6211\u5011\u7684\u5c08\u6848\u4e2d\u3002\u60a8\u53ef\u4ee5\u5f9e <a href=\"https:\/\/github.com\/ashleymills\/Reachability.swift\" target=\"_blank\" rel=\"noopener\">GitHub<\/a> \u4e0b\u8f09 Reachability.swift \u6a94\u6848\u4e26\u5c07\u5176\u7d0d\u5165\u60a8\u7684\u5c08\u6848\u3002<\/p>\n<h3>\u6b65\u9a5f\u4e00\uff1a\u5c0e\u5165 Reachability<\/h3>\n<p>\u5c07\u4e0b\u8f09\u7684 Reachability.swift \u6a94\u6848\u6dfb\u52a0\u5230\u60a8\u7684 Xcode \u5c08\u6848\u4e2d\uff0c\u4e26\u78ba\u4fdd\u5176\u5728\u60a8\u7684\u76ee\u6a19\u4e2d\u53ef\u7528\u3002<\/p>\n<h3>\u6b65\u9a5f\u4e8c\uff1a\u521d\u59cb\u5316 Reachability<\/h3>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u5be6\u73fe\u7db2\u8def\u72c0\u614b\u6aa2\u6e2c\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import Reachability\n\nlet reachability = try! Reachability()\n\nreachability.whenReachable = { reachability in\n    if reachability.connection == .wifi {\n        print(\"Reachable via WiFi\")\n    } else {\n        print(\"Reachable via Cellular\")\n    }\n}\nreachability.whenUnreachable = { _ in\n    print(\"Not reachable\")\n}\n\ndo {\n    try reachability.startNotifier()\n} catch {\n    print(\"Unable to start notifier\")\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f\u4e09\uff1a\u555f\u52d5\u901a\u77e5<\/h3>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u6703\u5efa\u7acb\u4e00\u500b Reachability \u7269\u4ef6\uff0c\u4e26\u8a2d\u5b9a\u7576\u7db2\u8def\u53ef\u9023\u63a5\u6216\u4e0d\u53ef\u9023\u63a5\u6642\u5404\u81ea\u57f7\u884c\u7684\u52d5\u4f5c\u3002\u6700\u5f8c\uff0c\u4f7f\u7528 <code>startNotifier()<\/code> \u65b9\u6cd5\u4f86\u555f\u52d5\u6aa2\u6e2c\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u60a8\u9047\u5230\u300cUnable to start notifier\u300d\u7684\u932f\u8aa4\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd Reachability.swift \u6b63\u78ba\u5c0e\u5165\u5c08\u6848\u3002<\/li>\n<li>\u6aa2\u67e5\u60a8\u662f\u5426\u5728\u4e3b\u57f7\u884c\u7dd2\u4e2d\u547c\u53eb <code>startNotifier()<\/code> \u65b9\u6cd5\u3002<\/li>\n<li>\u78ba\u4fdd\u60a8\u7684\u5c08\u6848\u5df2\u7372\u5f97\u7db2\u8def\u5b58\u53d6\u6b0a\u9650\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4f7f\u7528 Reachability \u4e0d\u50c5\u80fd\u6aa2\u6e2c\u7db2\u8def\u72c0\u614b\uff0c\u9084\u53ef\u4ee5\u6839\u64da\u7db2\u8def\u72c0\u614b\u9032\u884c\u52d5\u614b\u8abf\u6574\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728\u4f7f\u7528\u8005\u7684\u7db2\u8def\u72c0\u614b\u6539\u8b8a\u6642\uff0c\u81ea\u52d5\u66f4\u65b0 UI \u6216\u91cd\u65b0\u52a0\u8f09\u8cc7\u6599\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u7db2\u8def\u9023\u63a5\u6aa2\u6e2c-\ud83d\udcf6\ud83d\udcbb_1675474640.4226801.webp\" alt=\"Swift \u7db2\u8def\u9023\u63a5\u6aa2\u6e2c \ud83d\udcf6\ud83d\udcbb\" title=\"Swift \u7db2\u8def\u9023\u63a5\u6aa2\u6e2c, \u7db2\u8def\u9023\u63a5\u72c0\u614b, \u7db2\u8def\u9023\u63a5\u8cc7\u8a0a, \u958b\u767c\u8005, \u7db2\u8def\u61c9\u7528\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. Reachability \u5982\u4f55\u904b\u4f5c\uff1f<\/h3>\n<p>Reachability \u900f\u904e\u76e3\u63a7\u7cfb\u7d71\u7684\u7db2\u8def\u72c0\u614b\u8b8a\u5316\uff0c\u4e26\u4f7f\u7528\u56de\u547c\uff08closure\uff09\u4f86\u901a\u77e5\u958b\u767c\u8005\u7576\u7db2\u8def\u72c0\u614b\u6539\u8b8a\u6642\u7684\u60c5\u6cc1\u3002<\/p>\n<h3>2. \u5982\u4f55\u5728 Swift \u4e2d\u6aa2\u67e5\u7db2\u8def\u662f\u5426\u53ef\u7528\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 Reachability \u7684 <code>whenReachable<\/code> \u548c <code>whenUnreachable<\/code> \u5c6c\u6027\u4f86\u6aa2\u67e5\u7db2\u8def\u72c0\u614b\uff0c\u4e26\u6839\u64da\u4e0d\u540c\u7684\u72c0\u614b\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002<\/p>\n<h3>3. \u6709\u54ea\u4e9b\u5176\u4ed6\u7684\u7db2\u8def\u6aa2\u6e2c\u5de5\u5177\u53ef\u4ee5\u4f7f\u7528\uff1f<\/h3>\n<p>\u9664\u4e86 Reachability\uff0c\u60a8\u9084\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 <code>NWPathMonitor<\/code>\uff0c\u9019\u662f iOS 12 \u53ca\u4ee5\u5f8c\u7248\u672c\u5f15\u5165\u7684\u7db2\u8def\u6aa2\u6e2c\u5de5\u5177\uff0c\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\u548c\u9748\u6d3b\u6027\u3002<\/p>\n<p>\u900f\u904e\u4ee5\u4e0a\u6b65\u9a5f\u548c\u7bc4\u4f8b\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u5728 Swift \u4e2d\u9806\u5229\u5be6\u73fe\u7db2\u8def\u9023\u63a5\u6aa2\u6e2c\u529f\u80fd\uff0c\u63d0\u5347\u61c9\u7528\u7684\u4f7f\u7528\u9ad4\u9a57\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u4e86\u89e3Swift\u7db2\u8def\u9023\u63a5\u6aa2\u6e2c\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u6aa2\u6e2c\u7db2\u8def\u9023\u63a5\u7684\u72c0\u614b\uff0c\u4e26\u4e14\u53ef\u4ee5\u63d0\u4f9b\u7db2\u8def\u9023\u63a5\u7684\u8cc7\u8a0a\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u7684\u958b\u767c\u7db2\u8def\u61c9\u7528\u3002<\/p>\n","protected":false},"author":1,"featured_media":7347,"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-7348","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-\u7db2\u8def\u9023\u63a5\u6aa2\u6e2c-\ud83d\udcf6\ud83d\udcbb_1675474640.4226801.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Uw","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7348","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=7348"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7348\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7347"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}