{"id":279,"date":"2025-05-29T11:09:45","date_gmt":"2025-06-05T03:18:44","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=279"},"modified":"2025-06-05T11:09:45","modified_gmt":"2025-06-05T03:18:44","slug":"swift-scheme-deep-link-%e8%a7%a3%e6%9e%90-%e7%b0%a1%e6%98%93%e7%af%84%e4%be%8b-url%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-scheme-deep-link-%e8%a7%a3%e6%9e%90-%e7%b0%a1%e6%98%93%e7%af%84%e4%be%8b-url%e8%a7%a3%e6%9e%90\/","title":{"rendered":"Swift \u6df1\u5c64\u9023\u7d50\u89e3\u6790\u6559\u5b78 | 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<h2>Swift \u6df1\u5c64\u9023\u7d50\u89e3\u6790\u6559\u5b78<\/h2>\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u5728 Swift \u4e2d\u89e3\u6790\u6df1\u5c64\u9023\u7d50\uff08Scheme Deep Link\uff09\u3002\u6df1\u5c64\u9023\u7d50\u5141\u8a31\u7528\u6236\u901a\u904e\u9ede\u64ca\u7279\u5b9a\u7684 URL \u76f4\u63a5\u8df3\u8f49\u5230\u61c9\u7528\u7a0b\u5f0f\u7684\u67d0\u500b\u9801\u9762\u6216\u555f\u52d5\u7279\u5b9a\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 Line \u61c9\u7528\u7a0b\u5f0f\u7684 `line:\/\/msg\/text\/+\u6587\u5b57`\uff0c\u53ef\u4ee5\u76f4\u63a5\u50b3\u905e\u8a0a\u606f\u4e26\u555f\u52d5 Line \u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n<h3>\u6df1\u5c64\u9023\u7d50\u7bc4\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u7fa9\u6df1\u5c64\u9023\u7d50\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">stevenApp:\/\/test.com:80\/12345;param?account=1&amp;password=2#fragment\n<\/code><\/pre>\n<p>\u9019\u500b URL \u5305\u542b\u4e86\u5e7e\u500b\u90e8\u5206\uff0c\u8b93\u6211\u5011\u4f86\u9010\u4e00\u89e3\u6790\u5b83\u5011\u7684\u610f\u7fa9\u3002<\/p>\n<h3>\u76e3\u807d\u6df1\u5c64\u9023\u7d50\u4e8b\u4ef6<\/h3>\n<p>\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u6211\u5011\u9700\u8981\u5be6\u4f5c\u4e00\u500b\u65b9\u6cd5\u4f86\u76e3\u807d\u4e26\u8655\u7406\u9019\u4e9b\u9023\u7d50\u3002\u4ee5\u4e0b\u662f\u5be6\u4f5c\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {\n    let result = ApplicationDelegate.shared.application(app, open: url, options: options)\n    if url.host == nil {\n        return true\n    }\n    print(\"url: \\(url)\")\n    print(\"scheme: \\(url.scheme ?? \"\")\")\n    print(\"host: \\(url.host ?? \"\")\")\n    print(\"port: \\(url.port ?? 0)\")\n    print(\"pathComponents: \\(url.pathComponents)\")\n    print(\"query: \\(url.query ?? \"\")\")\n    print(\"fragment: \\(url.fragment ?? \"\")\")\n    return result\n}\n<\/code><\/pre>\n<h4>\u6df1\u5c64\u9023\u7d50\u5404\u90e8\u5206\u89e3\u6790<\/h4>\n<ul>\n<li>**scheme**: \u4f60\u6240\u8a2d\u5b9a\u7684\u61c9\u7528\u7a0b\u5f0f\u9023\u7d50\uff0c\u4f8b\u5982 Line \u662f `line:\/\/`\u3002<\/li>\n<li>**host**: \u4e3b URL\u3002<\/li>\n<li>**port**: \u9023\u63a5\u7684\u7aef\u53e3\u865f\uff0c\u82e5\u6c92\u6709\u5247\u70ba nil\u3002<\/li>\n<li>**pathComponents**: \u5b50\u5206\u5c64\u5730\u5740\u3002<\/li>\n<li>**query**: GET \u50b3\u9001\u7684\u53c3\u6578\u3002<\/li>\n<li>**fragment**: \u5206\u5c64\u90e8\u5206\u3002<\/li>\n<\/ul>\n<h3>\u7bc4\u4f8b\u8f38\u51fa<\/h3>\n<p>\u7576\u4f60\u900f\u904e\u6df1\u5c64\u9023\u7d50\u555f\u52d5\u61c9\u7528\u7a0b\u5f0f\u6642\uff0c\u4f60\u53ef\u80fd\u6703\u770b\u5230\u985e\u4f3c\u4ee5\u4e0b\u7684\u8f38\u51fa\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">url: stevenApp:\/\/test.com:80\/12345;param?account=1&amp;password=2#fragment\nscheme: stevenApp\nhost: test.com\nport: 80\npathComponents: [\"\/\", \"12345;param\"]\nquery: account=1&amp;password=2\nfragment: fragment\n<\/code><\/pre>\n<h3>URL Query \u89e3\u6790\u5668<\/h3>\n<p>\u64f4\u5c55 `URL` \u985e\u5225\u4f86\u89e3\u6790 URL \u4e2d\u7684 GET \u53c3\u6578\u4e26\u5c07\u5176\u8f49\u63db\u70ba\u5b57\u5178\u5f62\u5f0f\uff0c\u9019\u6a23\u66f4\u65b9\u4fbf\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">extension URL {\n    \/\/\/ \u89e3\u6790 URL \u7684 GET \u53c3\u6578\u8f49\u70ba Dictionary\n    var params: [String: String]? {\n        if let urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true) {\n            if let queryItems = urlComponents.queryItems {\n                var params = [String: String]()\n                queryItems.forEach {\n                    params[<span class=\"katex math inline\">0.name] =<\/span>0.value\n                }\n                return params\n            }\n        }\n        return nil\n    }\n}\n<\/code><\/pre>\n<h4>\u5728 AppDelegate \u4e2d\u4f7f\u7528<\/h4>\n<p>\u5728 `AppDelegate` \u4e2d\u6253\u5370\u89e3\u6790\u5f97\u5230\u7684\u53c3\u6578\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">print(\"url params: \\(String(describing: url.params))\")\n<\/code><\/pre>\n<h4>\u7bc4\u4f8b\u8f38\u51fa<\/h4>\n<p>\u7576\u89e3\u6790\u6210\u529f\u5f8c\uff0c\u8f38\u51fa\u53ef\u80fd\u6703\u986f\u793a\u70ba\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">url params: Optional([\"password\": \"2\", \"account\": \"1\"])\n<\/code><\/pre>\n<hr \/>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><br \/>\n<img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"\u6df1\u5c64\u9023\u7d50\u793a\u4f8b\u5716\u7247\" title=\"\"><br \/>\n<\/a><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u6df1\u5c64\u9023\u7d50\u6709\u4ec0\u9ebc\u61c9\u7528\u5834\u666f\uff1f<\/h3>\n<p>A1: \u6df1\u5c64\u9023\u7d50\u53ef\u7528\u65bc\u884c\u92b7\u6d3b\u52d5\u3001\u63a8\u5ee3\u7279\u5b9a\u5167\u5bb9\u3001\u7528\u6236\u518d\u884c\u5c0e\u5411\u53ca\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u7b49\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u6e2c\u8a66\u6df1\u5c64\u9023\u7d50\u529f\u80fd\u662f\u5426\u6b63\u5e38\uff1f<\/h3>\n<p>A2: \u53ef\u4f7f\u7528 Xcode \u7684 Debug \u6a21\u5f0f\uff0c\u6216\u901a\u904e\u5176\u4ed6\u61c9\u7528\u7a0b\u5f0f\uff08\u5982 Safari\uff09\u9ede\u64ca\u6df1\u5c64\u9023\u7d50\u4f86\u6e2c\u8a66\u3002<\/p>\n<h3>Q3: \u6df1\u5c64\u9023\u7d50\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u7684\u5dee\u7570\u662f\u4ec0\u9ebc\uff1f<\/h3>\n<p>A3: \u5728 iOS \u548c Android \u4e0a\uff0c\u6df1\u5c64\u9023\u7d50\u7684\u5be6\u4f5c\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\uff0c\u9700\u91dd\u5c0d\u4e0d\u540c\u5e73\u53f0\u9032\u884c\u76f8\u61c9\u7684\u8a2d\u5b9a\u8207\u8655\u7406\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9ede\u64caURL\u8f49\u8df3APP\u7684\u67d0\u9801\u9762\u6216\u662f\u555f\u52d5\u67d0\u9805\u529f\u80fd<br \/>\n\u6bd4\u5982Line\u6240\u91cb\u51fa\u7684 line:\/\/msg\/text\/+\u6587\u5b57<br \/>\n\u53ef\u4ee5\u50b3\u9001\u8a0a\u606f\u76f4\u63a5\u5e36\u6587\u5b57\u904e\u53bbLine APP\u555f\u52d5<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[76,75,77],"class_list":["post-279","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","tag-deep-link","tag-scheme","tag-77","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-4v","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/279","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=279"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/279\/revisions"}],"predecessor-version":[{"id":998,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/279\/revisions\/998"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}